Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?

Shebang Inside the coding ?

by vennirajan (Friar)
on Jan 16, 2006 at 05:40 UTC ( #523394=perlquestion: print w/replies, xml ) Need Help??
vennirajan has asked for the wisdom of the Perl Monks concerning the following question:

Dear Monks,

I have seen sha bang type of coding, for example

#! $someVar = $obj->some_method();

I know that #!/usr/bin/perl this is sha bang and its functionalities. My doubt is why they are putting the above sort of coding in between the codes. My editor ( vim ) also high lighting in different color. Does it mean any other meaning or it just a comment or any thing else ? Share your ideas.

Thanks in advance.

S.Venni Rajan.
"A Flair For Excellence."
                -- BK Systems.

2006-01-17 Retitled by planetscape, as per Monastery guidelines
Original title: 'Sha Bang Inside the coding ?'

Replies are listed 'Best First'.
Re: Shebang Inside the coding ?
by edoc (Chaplain) on Jan 16, 2006 at 05:51 UTC
    It's just a comment.



      If it is a comment means, then why does my editor (vim) showing in different color (green) ? ( White color for comments ) ?!?!?!?!
      Doest it meant with editor ?

      S.Venni Rajan.
      "A Flair For Excellence."
                      -- BK Systems.

        Syntax colorers sometimes get confused. It probably sees the #! at the beginning of any line instead of only matching for the first line. I'll have to add this to my list of things that are hard to do with syntax highlighters. :)

        brian d foy <>
        Subscribe to The Perl Review

        Because your editor, whatever it is, however good it is, is not perl, and it is a well known axiom that "nothing but perl can parse Perl". So do not trust your editor's syntax highlighter. Trust your perl parser! Of course this simple case is on the border line, so to say, and probably other syntax highlighters do a better job of only marking a shebang looking like line like a shebang if it actually occurs at the beginning of the script. But in general expect similar "mistakes".

        Occasionally even perl has a difficulties parsing Perl and you need to help it by disambiguating suitably!

        Of course both the shebang line and a "regular" comment are just comments to perl itself. Well, not quite: I remember having read that perl does not fully trust the OS interpreting the shebang line and re-reads it, which is the reason why stuff like

        #!Perl -lpi.bak

        does work also under (osen that do not do shebang line interpretation, like) Windows.

Re: Shebang Inside the coding ?
by sk (Curate) on Jan 16, 2006 at 05:57 UTC
    Shebangs is meaningful only on the first 2 bytes in the first line of a file.

    #!/usr/bin/perl print "hi\n";

    call that prog "hi" and try to chmod +x on it and run it. It will work fine. (of course assuming the path to perl is correct) Now add a blank line above #!. Try to run this again. What do you see? Also try to add a space before shebang. what happens now?

    Vim is not looking to see if the shebang is on the first line or not. That's why it is coloring it.

    In short, that line is just a comment if it is in the middle of the code. -SK

    Update: Please see ysth's reply on how shebang can have other stuff before it.

      Shebangs is meaningful only on the first 2 bytes in the first line of a file.
      Not always. Perl's -x switch allows you to run a program that has other garbage before it (e.g. the program is embedded in an email message), and expects to find the #! line partway into the file. See just above
        Thanks ysth. I totally forget about that switch. Now I remember how people use to make jpeg files print JAPH but putting the shebang at the end of the jpeg file and running the jpeg file through -x. Thanks for catching this!



      Dear All,
      Thanks For Sharing your Ideas with Me. I got enough knowledge about sha bang as well its functionalities.

      S.Venni Rajan.
      "A Flair For Excellence."
                      -- BK Systems.</b
Re: Shebang Inside the coding ?
by helphand (Pilgrim) on Jan 16, 2006 at 05:52 UTC

    AFAIK, the shebang has meaning only as the first two characters of an executable text file, the shell will interpret what follows as the path to the program to execute, at least under *nix.

    If you are seeing the #! used elsewhere in the script, then it is simply some weird commenting scheme since everything after the # to the end of line will be treated as a comment.


Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://523394]
Approved by Old_Gray_Bear
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others contemplating the Monastery: (5)
As of 2017-06-25 14:43 GMT
Find Nodes?
    Voting Booth?
    How many monitors do you use while coding?

    Results (567 votes). Check out past polls.