tags:

views:

76

answers:

2

I have perl scripts starting with #!/usr/bin/perl or #!/usr/bin/env perl

First, what does the second version mean?

Second, I use Ubuntu. All the scripts are set as executables. When I try to run a script by simply invoking it's name (e.g. ./script.pl) I get : No such file or directory. when I invoke by perl ./script.pl it runs fine.

Why?

+2  A: 

Using #!/usr/bin/env perl gets around the problem of perl not necessarily being in /usr/bin on every system; it's just there to make the script more portable


On a related note, for your second problem, is there a /usr/bin/perl and/or /usr/bin/env? If not, that would explain why running the scripts directly doesn't work; the shebang isn't handled if you run the script as an argument to perl

Michael Mrozek
Just to clarify - if you run `perl script` it ignores any path in the shebang line but it does look at any option flags in the line.
justintime
Just to clarify the clarification with an edge case: If you run "perl script" it ignores any path in the shebang line if (and only if) the shebang line contains the word "perl". If you put "#!/bin/sh" in there, perl is clever enough to execute sh for you.
Mark Fowler
+3  A: 

The #!/usr/bin/env perl uses the standard POSIX tool env to work around the "problem" that UNIX doesn't support relative paths in shebang lines (AFAIK). The env tool can be used to start a program (in this case perl) after modifying environment variables. In this case, no variables are modified and env then searches the PATH for Perl and runs it. Thus a script with that particular shebang line will work even when Perl is not installed in /usr/bin but in some other path (which must be in the PATH variable).

Then, you problem with ./script.pl not working: you said it has the executable bit(s) set, like with chmod +x script.pl ? But does it also start with a shebang (#!) line ? That is, the very first two bytes must be #! and it must be followed by a file path (to perl). That is necessary to tell the kernel with which program to run this script. If you have done so, is the path correct ? You want to try the #!/usr/bin/env perl variant ;-)

DarkDust
the very first line of the scripts is `#!/usr/bin/env perl`. I also tried replacing it with `#!/usr/bin/perl` (which indeed exists on my system) but that too didn't work with giving the same error message.
David B
@David B: what `which env` and `which perl` say?
Dummy00001
@Dummy00001: `$ which env` => `/usr/bin/env`, `$ which perl` => `/usr/bin/perl`
David B