Spidy has asked for the wisdom of the Perl Monks concerning the following question:

Does anyone know how I would go about doing this? I haven't managed to figure it out yet, I'm trying to find where I'd need to put my files, although I'm pretty sure that I actually type 'perl myscript.pl' in the Terminal. If anyone knows how I would actually run my scripts/where I would put them to run them, I would be much obliged.

Thanks,
Spidy

Replies are listed 'Best First'.
Re: Running Perl Scripts on a Mac?
by brian_d_foy (Abbot) on Mar 14, 2005 at 23:25 UTC

    If you are on Mac OS X, then it's just like any other unix box. You can put your scripts whereever you like. You just have to tell perl where to find it.

    $ perl hello.pl $ perl some/dir/hello.pl $ perl /Users/you/Desktop/hello.pl
    --
    brian d foy <bdfoy@cpan.org>
Re: Running Perl Scripts on a Mac?
by punkish (Priest) on Mar 14, 2005 at 23:02 UTC
    Macs (assuming you are talking about Mac OS X) come ready to run Perl without any standing-on-your-head involved. I have a folder called "Projects" (you can also use your "Sites" folder, esp if your work is web related) and I put my Perl programs in appropriate subfolders therein.

    Write you scripts using any editor that will utilize Unix line endings (most all will... skEdit or TextWrangler or Smultron), save them, go to the terminal and chmod +x yourscript.pl, and then perl yourscript.pl

    update: added links to text editors

    --

    when small people start casting long shadows, it is time to go to bed
Re: Running Perl Scripts on a Mac?
by jdalbec (Deacon) on Mar 15, 2005 at 00:23 UTC
    • If you're going to run perl yourscript.pl then chmod +x yourscript.pl is not necessary.
    • Mac OS X and other *n*x systems require a full path in the #! line.
    • Putting . in your path is a bad habit. This can cause you to execute files in the current directory without meaning to. Just type ./yourscript.pl instead.
Re: Running Perl Scripts on a Mac?
by Fletch (Bishop) on Mar 15, 2005 at 03:08 UTC

    As has been said, if you're at a shell prompt OS X is pretty much like any other *NIX flavour. If you're looking for something you can click on from the Finder (or drop files onto) then you want to google for "DropScript" or "Platypus". These will wrap your perl in an OS X application and give it the right structure to run when you click on it.

Re: Running Perl Scripts on a Mac?
by sh1tn (Priest) on Mar 14, 2005 at 22:04 UTC
    Make sure the perl executable is in your $PATH. And also - it must be:
    perl -e myscript.pl
    Update: Errto corrected this ugly mistake.
    # corrected: perl -e 'code' perl 'script'


      No this is not correct. Perl -e is for specifying the program text on the command line. Like
      perl -e 'print "foo\n";'
      For simply running a script
      perl script.pl
      is correct.
Re: Running Perl Scripts on a Mac?
by larryp (Deacon) on Mar 14, 2005 at 22:07 UTC

    You can put the scripts wherever you like on your system. However, the location should be in your PATH environmental variable, or you'll have to type the complete path to your file.

    In addition, you need to make sure the files have execute permissions and that they include the shebang line as the first line (#!/usr/bin/perl) in the file.

    Once you've done that, you type perl scriptname.pl on the command line in the terminal. That's all you need. :)

    HTH,

    /Larry

    Update: Fixed a couple of typos. :)

      One more typo -- you'd type scriptname.pl after you've followed the rest of your instructions. As with any other perl box, you can run scripts one of two ways from the command line:

      Placing the files in your $PATH.
      If you want to call the file as the name of the file (eg, 'script.pl', then you'll want to use the first two paragraphs -- place the file in your $PATH, make sure it has execute (chmod u+x script.pl), and the proper shebang line... but you call it without calling perl on the command line.
      Calling the perl executable with an argument.
      This will not search your path, and will only search your local directory (or whatever path you specify. When called this way, you need perl in your path, but not the script. Also, the script doesn't need a shebang line, or be executable.

      Update: if you want to make sure perl is in your path, type which perl at the command line. It's installed by default at /usr/bin/perl (but without critical header files that are needed to build new modules, at least in 10.3 server, as I found out today).

      so, how would I go about finding out what's in my $PATH variable, using a one-liner?

        Open Terminal, and type the following at the commahd line:

        echo $PATH

        Letter case counts. :) Terminal will return a colon-separated string that contains your path information. You could put your scripts into any of the directories listed, however, I would recommend that you create a 'bin' directory in your Home directory and put your scripts there. Then, to execute your scripts, you'd type the following on the command line:

        perl $HOME/bin/scriptname.pl

        or

        perl /Users/yourusername/bin/scriptname.pl

        You can add directories to your PATH variable, but I'll leave that exciting task to you. (Hint: On the command line, type man bash.) :)

        HTH,

        /Larry

Re: Running Perl Scripts on a Mac?
by cbrandtbuffalo (Deacon) on Mar 15, 2005 at 00:07 UTC
    If you want to run scripts without typing the 'perl' part, make sure the first line of your script is a proper shebang line like so:
    #!/usr/bin/perl -w
    Also add dot '.' to your path and make the script executable. With dot in your path, you can run any executable from the directory you are in. To add dot to your path with the tcsh shell, add this to your .cshrc file:
    setenv PATH ${PATH}:.