Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

Re: Portable Shebang

by VSarkiss (Monsignor)
on Oct 12, 2001 at 17:45 UTC ( [id://118474]=note: print w/replies, xml ) Need Help??


in reply to Portable Shebang

In the original BSD Unix, and current versions of Linux, the shebang is not implemented by the shell, but by the execve system call. When the file that's handed to execve is not a binary executable (as found from its magic number), then the call checks whether the first two characters are #!. If so, it attempts to exec the file named immediately afterwards instead, passing it the original file as input. The path to the file has to be valid -- there is no search of $PATH, which is maintained by the shells, not at the kernel level. Hence, your example of #!perl would work only when the Perl executable was in the current directory of the process that was calling exec.

What can add to the confusion is that Perl will actually examine a shebang-looking line to see if it should "fake" a shebang, for systems that either don't implement it (Win32 comes to mind), or which have a crippled version that doesn't allow passing arguments. You can get much more detail on this behavior in the opening paragraphs of perlman:perlrun. The /usr/bin/env perl trick is described there as well.

HTH

Replies are listed 'Best First'.
Re: Re: Portable Shebang
by stefan k (Curate) on Oct 12, 2001 at 18:12 UTC
    Thanks for the pointer to perlman:perlrun - that was a good read.

    Just one more question maybe: how does one get the -w switch when using the /usr/bin/env trick?

    Regards... Stefan
    you begin bashing the string with a +42 regexp of confusion

      Hmm.... I couldn't coax it to do that on my Linux box. It appears env expects the shell to have split up the arguments before they're handed to it, so when execve hands it one long string of file and arguments, it thinks the blanks are part of the file name. That's judging from the error message I get: env: perl -w: No such file or directoryThat's not such a big deal with -w since you can substitute use warnings, but is a problem with -T, which has to appear early on. There may be some clever way to do with the env trick. I think I'll just stick to regular ol' shebangs....

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://118474]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others exploiting the Monastery: (4)
As of 2024-04-18 02:01 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found