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

How do I run a Perl script on OS X?

I honestly can't find the answer anywhere! Presumably I have to run a command in Terminal but what?

(I know this is a real basic and stupid question)


Solution 1:

You can run your Perl script by invoking the Perl interpreter and giving your file as input:

perl myprogram.pl

Solution 2:

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 3:

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