How to find out the location of the perl interpreter? First line always #!/usr/bin/perl?

M

Mark Richards

I can run perl scripts on certain computers with this first line:

#!/usr/bin/perl

However on other computers this does not work.

AFAIK this line represent the location of the perl interpreter.

How can I find out the correct path to the perl interpreter resp. which
path I have to enter in the first line ?

Mark
 
M

Mark Shroyer

I can run perl scripts on certain computers with this first line:

#!/usr/bin/perl

However on other computers this does not work.

AFAIK this line represent the location of the perl interpreter.

How can I find out the correct path to the perl interpreter resp. which
path I have to enter in the first line ?

Common practice is to use the following hashbang instead:

#!/usr/bin/env perl

or

#!/usr/bin/env perl -options

The utility env(1)'s purpose is ostensibly to run programs with
certain specified environment variable bindings, which are passed to
env as name=value pairs preceding the name of the command to run;
but when used this way, it simply execs the first copy of "perl"
that it finds on the path, passing it any options specified
thereafter.

On almost all modern, Unix-like (including Linux, *BSD, and OS X)
systems, env is either installed at /usr/bin/env or has a symbolic
link to it from that path. On older versions of Caldera OpenServer,
however, and maybe a couple other now-obscure operating systems,
there's only /bin/env; but the existence of /usr/bin/env is
widespread enough these days to make it an extremely portable (and
popular) choice.

On the Unix systems I regularly use, anyway (OpenBSD, OS X, and
Debian Linux), the hashbang has to specify a *full* interpreter
path, which is the reason why the name "perl" cannot simply be put
in there without proxying it through an absolutely-referenced
command in this manner.
 
A

A. Sinan Unur

....

On almost all modern, Unix-like (including Linux, *BSD, and OS X)
systems, env is either installed at /usr/bin/env or has a symbolic
link to it from that path.

On almost all modern, Unix-like (including Linux, *BSD, and OS X)
systems, system default perl is either installed at /usr/bin/perl
or has a symbolic link to it from that path.

Sinan
 
M

Mark Shroyer

On almost all modern, Unix-like (including Linux, *BSD, and OS X)
systems, system default perl is either installed at /usr/bin/perl
or has a symbolic link to it from that path.

Apparently not on the OP's computers, as you would learn if you took
the time to read his post. And hard-coding the interpreter to
/usr/bin/perl is a bad idea anyway, in case the user wants to
default to a different Perl installation in /usr/local or somewhere
else entirely.
 
B

Ben Morrow

Quoth (e-mail address removed):
which perl, on most Unix systems. Also, perl -V:startperl will tell you
the correct #! line to use, if perl could determine one. If it couldn't,
then perl -V:perlpath will give you the full path to this perl.

The best answer, though, is to package up your scripts into a proper
distribution, and use the EXE_FILES parameter to ExtUtils::MakeMaker (or
equivalent for Module::Build or Module::Install), which will fixup the
#! line for you as required, including doing something appropriate for
systems that don't support #! at all.
Common practice is to use the following hashbang instead:

#!/usr/bin/env perl

This is a potential security risk: you are allowing the user of the
script to determine which perl is used. I don't know how env behaves
when setid or under sudo: unless you do, you shouldn't use this.

Ben
 
X

xhoster

I can run perl scripts on certain computers with this first line:

#!/usr/bin/perl

However on other computers this does not work.

AFAIK this line represent the location of the perl interpreter.

How can I find out the correct path to the perl interpreter resp. which
path I have to enter in the first line ?

Ask the system administrator.

Xho

--
-------------------- http://NewsReader.Com/ --------------------
The costs of publication of this article were defrayed in part by the
payment of page charges. This article must therefore be hereby marked
advertisement in accordance with 18 U.S.C. Section 1734 solely to indicate
this fact.
 
A

A. Sinan Unur

Apparently not on the OP's computers, as you would learn if you took
the time to read his post.

Well, if there is no /usr/bin/perl on the OP's system, what are the odds
that /usr/bin/env is there?
And hard-coding the interpreter to /usr/bin/perl is a
bad idea anyway, in case the user wants to
default to a different Perl installation in /usr/local or somewhere
else entirely.

That can be customized during installation.

Sinan
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,756
Messages
2,569,534
Members
45,007
Latest member
OrderFitnessKetoCapsules

Latest Threads

Top