'How can I run a Perl script on Mac OS X?

How do I run a Perl script on OS X?



Solution 1:[1]

The easiest way to run a perl script is with the option:

perl myprogram.pl

However, you may find it more useful to add a shebang line at the top of the perl file.

#!/usr/bin/perl
print "Hello World!\n";

In order to execute this script, you need to add execute permissions to your program. Run:

chmod +x myprogram.pl

Now, in order to run your script, you can simply type:

./myprogram.pl

Solution 2:[2]

A good tutorial on Perl in OSX can be found here:

http://www.mactech.com/articles/mactech/Vol.18/18.09/PerlforMacOSX/index.html

A generic documentation on executing Perl code is of course perldoc perlrun.

To answer your question directly:

You can run a perl script on any Unix system by either having the code evaluated and executed from command line:

perl -e 'print "Hello World\n"';

Or you can save your Perl script to a file (customarily having .pl extension, say script1.pl and with the first line being #!/usr/bin/perl) and then you can execute it as any Unix program (after setting proper execute permissions)

/path/to/script/script1.pl

You can also execute a script from a file by running perl interpreter as the command and giving the script as a parameter (in this case execute permissions to the script are not needed):

perl /path/to/script/script1.pl

Solution 3:[3]

For some reason the whole directory didn't work for me but I just did

perl ~/Desktop/file.pl

(you could also use any folder that is in your user-folder after the ~/)

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Jeff Catania
Solution 2
Solution 3 Dharman