tags:

views:

188

answers:

5

My Perl script runs under several OS's, including unix and Win32. On every OS there is unique Perl setup, whereby the perl interpreter is located under different directories.

Is there a way to specify the interpreter on the shebang line like:

#!which perl
A: 

This is discussed at http://www.perlmonks.org/index.pl?node_id=118460

In short, whatever you do, you are going to end up assuming a path to a binary at some point or another, and if /usr/bin/perl doesn't work, then Perl has been misconfigured.

(Windows doesn't care about the shebang line)

David Dorward
I agree with the last paragraph, but the second is wrong. On many systems where I run perl I do not have admin rights and have no power to touch /usr/bin/perl, so I install my own in a different location. These installations have no trouble whatsoever referrign to themselves instead of to the preexisting Perl in /usr/bin/perl.
reinierpost
The second paragraph is somewhat tongue in cheek, and references the linked document.
David Dorward
in my case the perl interpreter is installed under user's home directory.
D_K
+1  A: 

man perlrun command gives some answers to this question, but none are fully portable.

mouviciel
+2  A: 

On some unixes (and presumably the likes of Cygwin and MinGW/Msys) you can use env to search for perl on the PATH like this:

#!/usr/bin/env perl
Ned
This finds a perl, but maybe not the one you want. If you want a particular one, this isn't the way to specify it.
brian d foy
+2  A: 

One trick used is to have a line

 #!/usr/bin/env perl

which runs Perl for you regardless of where it is located.

But ... you should note that this can lead to problems if you have more than one Perl interpreter on the system. For example I use Ubuntu linux and Perl is a component of the system. But the Ubuntu system Perl interpreter is not in good shape, due to some dubious setup decisions by the Debian people, so installing things like CPAN modules is tricky. Thus I always use my own compiled perl in my user space for my own work. Some of the Ubuntu Perl scripts use the above form and thus break if I happen to run them from the command line, because my perl interpreter is ahead in the path of the Ubuntu default Perl.

Kinopiko
This finds a perl, but maybe not the one you want. If you want a particular one, this isn't the way to specify it.
brian d foy
I know. But it will give you the same result as `which perl`, which is what the question asked for.
Kinopiko
I do this (with a Perl script) for all my scripts, and I find it the best solution. But I usually run them under Cygwin when I'm on Windows.
reinierpost
I think it's worth pointing out some problems with this though.
Kinopiko
You also need to note that this is PATH dependent, so you need to ensure that the right PATH shows up everywhere, for all users, in cron, and so on.
brian d foy
If you have enough control to make sure you always get the right PATH, presumably there are other ways to solve the problem - like linking to the right perl from a known location or patching the script to have the correct path.
Ned
+7  A: 

Install your scripts with the normal CPAN tools. During the build, they take the perl that you used to run the build script and inserts it into the shebang line. Also, you'll ensure that the perl you are using has the dependencies installed.

For instance, you have at the top of your script:

#!perl

Using the normal module distribution process (see my App::PPI::Dumper as an example),

$ perl5.10.0 Makefile.PL

Installing it, ExtUtils::Makemaker or Module::Build rewrites the shebang with the path of the perl you used with the build script:

 #!/usr/local/perls/perl-5.10.0-rc2/bin/perl

 eval 'exec /usr/local/perls/perl-5.10.0-rc2/bin/perl  -S $0 ${1+"$@"}'
     if 0; # not running under some shell

When you distribute your script as a complete distribution, you get all of the toolchain features for free, like automatic installation of dependencies. It makes life much easier.

brian d foy