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

PerlMacos

Perl Problem Overview


How do I run a Perl script on OS X?

Perl Solutions


Solution 1 - Perl

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

perl myprogram.pl

Solution 2 - Perl

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 - Perl

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 4 - Perl

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 ~/)

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionJeffView Question on Stackoverflow
Solution 1 - PerlcodaddictView Answer on Stackoverflow
Solution 2 - PerlJeff CataniaView Answer on Stackoverflow
Solution 3 - PerlDVKView Answer on Stackoverflow
Solution 4 - Perlcoding_duckView Answer on Stackoverflow