Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

What's #! got to do with it?

by Petras (Friar)
on Jan 16, 2004 at 21:13 UTC ( #321907=perlquestion: print w/ replies, xml ) Need Help??
Petras has asked for the wisdom of the Perl Monks concerning the following question:

Hi, everybody!

I was writing a script for a friend on his computer, and his perl path is different than mine, which is also different from the perl path on my server. So, I wondered, "I wonder if I can put multiple shebangs in a script to make it more portable?" Several hundred xp points ago I would have simply posted that question under SOPW, but this time I decided to try it and see if it worked (before asking ;)

I wrote a simple "Hello, world!" with multiple shebangs, and the script worked! I thought, being so basic, the program might not have even needed to know where perl is (after, the perl command should deal with that, right?). So, I went to the best node of all times and stole a much more complicated bit of code. Still worked with multiple shebangs. Good stuff.

Here's where it got interesting. I tried #! /usr/reallybadpath/perl -w for the only shebang, and the script still worked. So, I ditched the shebang all together, and the camel script still worked. Interesting.

So, is what I learned early on in perl program no longer true with the rise of Perl 5.8.x? Is a shebang still something to be all sharp and bangy about? What gives?

Cheers!
Petras
Don't worry about people stealing your ideas. If your ideas are any good, you'll have to ram them down people's throats.

-Howard Aiken

Comment on What's #! got to do with it?
Download Code
Re: What's #! got to do with it?
by b10m (Vicar) on Jan 16, 2004 at 21:19 UTC

    The shebang specifies what kind of program needs to interpret the code. Besides Perl, this can be python, bash, sh, etc. Dropping the shebang, makes your shell clueless. What to do with the code?

    Presumably, you tried $ perl script instead of $ ./script. In the first example, you just feed perl the script. In the second case, you tell your shell: "Hey, execute this script!". In that case, it would be nice if your shell knew what kind of code it was :)

    --
    b10m
Re: What's #! got to do with it?
by hardburn (Abbot) on Jan 16, 2004 at 21:20 UTC

    I bet you're invoking the program on the shell like:

    $ perl file.pl

    In which case the shebang doesn't matter, and never has. It only matters if you're doing:

    $ ./file.pl

    Or if you're executing it on a web server without an embedded interpreter (like mod_perl).

    The shebang is a magic number (see man magic) which tells the operating system to execute the command on the line with the given options to intepret the rest of the file. Since the perl file.pl form already has an interpeter attached to the code, the #! is considered just another comment (sort of . . . )

    ----
    I wanted to explore how Perl's closures can be manipulated, and ended up creating an object system by accident.
    -- Schemer

    : () { :|:& };:

    Note: All code is untested, unless otherwise stated

      Well, this is what happens when one is assimilated. When I ran .\hello.pl from a command (DOS) prompt, notepad opened with the script for editting :)

      Rats, foiled again!
      Petras

      UPDATE: Even with correct shebang, calling the script from the DOS prompt via  .\hello.pl in win XP still launches notepad. Kinda makes you wanna launch windows...
      Don't worry about people stealing your ideas. If your ideas are any good, you'll have to ram them down people's throats.

      -Howard Aiken
        The shebang line doesn't matter on DOS/Windows, except for (CGI-)scripts for under Apache.

        You can fix that by changing the default action associated with the .pl extension. To do that you need to go to: explorer->tools->folder options->File Types->Extensions->PL->Advanced->Open->Edit

        If you set that to something like c:\Perl\bin\perl.exe" "%1" %* you will automatically invoke the perl interpreter rather than notepad.

        From this interface you can also add a separate option to be shown on right-click context menus. For example, you can set "Edit" as an option that uses notepad, but have it default to "Open" with the interpreter.

Re: What's #! got to do with it?
by Zaxo (Archbishop) on Jan 16, 2004 at 21:20 UTC

    Is this, perchance, on Win32? Windows ports only look at the shebang line to match 'perl' and some run options.

    If you have multiple shebang lines working on Unix, I'd sure be interested to see how you do it.

    After Compline,
    Zaxo

Re: What's #! got to do with it?
by Coruscate (Sexton) on Jan 16, 2004 at 21:20 UTC

    I'm going to assume you were executing the script as "perl foo.pl" on the command line. If so, you are executing the perl executable (which just happens to be the same path as you'd put on the shebang line). The shebang line becomes necessary if you want to execute the script via "./foo.pl" on the command line. Because in this case, you are executing the actual script itself (via the shell), and the shell has to know which program to use to interpret the file.

    Essentially, try one of your "magic" scripts by executing a "chmod 755 foo.pl" on it and then run "./foo.pl". It won't work (unless the valid perl path is placed as the first shebang line). The rest of the shebang lines aren't actually shebang lines. perl comments begin with '#', so all other "shebang lines" are really just plain old comments.

      so all other "shebang lines" are really just plain old comments.

      Not quite. It might still try to parse that line for options. Which is why you can still get the Too late for "-T" option at taint_test.pl line 1. error when calling a program as perl file.pl with -T set on the file's shebang line.

      ----
      I wanted to explore how Perl's closures can be manipulated, and ended up creating an object system by accident.
      -- Schemer

      : () { :|:& };:

      Note: All code is untested, unless otherwise stated

        Not quite. It might still try to parse that line for options. Which is why you can still get the Too late for "-T" option at taint_test.pl line 1. error when calling a program as perl file.pl with -T set on the file's shebang line.

        That's why I said qq{so all the other "shebang lines"} (ie: any shebang lines following the first one) :)

Re: What's #! got to do with it?
by Plankton (Priest) on Jan 16, 2004 at 21:21 UTC
    Maybe if you had of done a search for #! you might of found this node.

    Plankton: 1% Evil, 99% Hot Gas.
Re: What's #! got to do with it?
by TomDLux (Vicar) on Jan 17, 2004 at 16:11 UTC

    Since you use a Unix path in your example, I'll assume all the people referrring to Windows are on the wrong path.

    Try your experiment again, but begin with the shell command appropriate to your shell:

    PATH=/usr/bin:/usr/local/bin # bash, etc setenv PATH "/bin:/usr/bin:/usr/local/bin" # csh, etc

    and see what happens.

    --
    TTTATCGGTCGTTATATAGATGTTTGCA

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (14)
As of 2014-07-23 16:08 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite superfluous repetitious redundant duplicative phrase is:









    Results (146 votes), past polls