Beefy Boxes and Bandwidth Generously Provided by pair Networks Joe
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

/usr/bin/env perl - query

by opensourcer (Monk)
on Oct 13, 2008 at 05:28 UTC ( #716740=perlquestion: print w/ replies, xml ) Need Help??
opensourcer has asked for the wisdom of the Perl Monks concerning the following question:

Hi,

what's the difference between /usr/bin/env perl and /usr/bin/perl.
suppose if the i have three differnt versions of perl who i switch between using /usr/bin/env perl, rather then mentioning the path in shebang.

Thanks.

Comment on /usr/bin/env perl - query
Re: /usr/bin/env perl - query
by moritz (Cardinal) on Oct 13, 2008 at 05:36 UTC
    The main difference ist that /usr/bin/perl is a fixed path to one perl (which is usually the one that ships with your operating system), while /usr/bin/env perl will take the first perl in $PATH.

      I know it is OT, but ... does /usr/bin/env perl makes much sense at all? In this case, I could simply write perl. I feel that env is mainly useful when used together with options, such as env -i for providing an empty environment for a command...

      -- 
      Ronald Fischer <ynnor@mm.st>
        You are right that /usr/bin/env perl and perl will behave the same way on the command line. However, as JavaFan points out below, they are quite different when used on the shebang line (since the former will look for perl in your path, while the latter won't).
Re: /usr/bin/env perl - query
by j1n3l0 (Pilgrim) on Oct 13, 2008 at 08:46 UTC
    I've also noted that when you use:

    #! perl

    you cannot then run the program like this:

    ./your-program.pl


    Smoothie, smoothie, hundre prosent naturlig!
      And rightly so, assuming you're on Unix.

      On Unix, the first two bytes of an executable are "magical" - they tell the kernel what kind of executable it is. It may be a binary (depending on the OS, it may be able to run different styles of binaries), or it may be something that's to be fed to an interpreter. The magical bytes to signal that are 0x23 0x21, which in ASCII looks like "#!". Then the kernel takes the rest of the line (up to the next horizontal whitespace, or newline) as the name of the interpreter to run, and then feed the program as its input.

      So, if your program starts with

      #! perl
      and execute the program, the kernel will try to execute "perl" (assuming your OS skips leading whitespace; your program is more portable if it doesn't have whitespace after the #!). But the kernel isn't going to peek in your environment settings, and won't try all the directories in your $PATH. So it won't find "perl".
Re: /usr/bin/env perl - query
by toolic (Chancellor) on Oct 13, 2008 at 14:07 UTC

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://716740]
Approved by moritz
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others about the Monastery: (15)
As of 2014-04-17 13:52 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    April first is:







    Results (449 votes), past polls