Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

Clarify My doubt on #!/usr/local/bin/perl

by kprasanna_79 (Hermit)
on Jun 01, 2005 at 05:09 UTC ( #462292=perlquestion: print w/replies, xml ) Need Help??

kprasanna_79 has asked for the wisdom of the Perl Monks concerning the following question:

Greatings Monks,

I have seen in many books and online help, that the first line of ur perl code should be

#!/usr/local/bin/perl

I know why this is used for and i have used this line, when i code in linux platform. Is it necessary to put this line while doing code in windows platform.


And in that line why (!) symbol is used. This i am asking out my interest, because /usr/local/bin/perl is the perl location path and # is to represent comment, but why ! symbol is used


Thanks in advance
--Prasanna.K

Replies are listed 'Best First'.
Re: Clarify My doubt on #!/usr/local/bin/perl
by holli (Abbot) on Jun 01, 2005 at 05:16 UTC
    Is it necessary to put this line while doing code in windows platform.
    It is not necessary. The perl interpreter to be used in Windows is determined by the file associations of the system. But it is not completely useless. You can pass switches to perl via the shebang line like
    #!perl -w
    which switches warnings on.


    holli, /regexed monk/
Re: Clarify My doubt on #!/usr/local/bin/perl
by tlm (Prior) on Jun 01, 2005 at 05:24 UTC

    The shebang (or bang) line is not specific to perl. In, say, Bourne shell scripts you see this on the first line:

    #!/bin/sh
    See this article for a quick description. I imagine that a lengthier description would require delving into the history of shells and interpreters, but there is no profound significance to the "!".

    The term "bang" comes from equating the exclamation symbol "!" with a loud noise. "shebang" is American slang; it's used in the expression "the whole shebang", meaning "the whole thing". As far as I know its meaning has nothing to do with interpreters; it just sounds similar to "bang", so it got adopted by extension, I imagine.

    the lowliest monk

Re: Clarify My doubt on #!/usr/local/bin/perl
by monarch (Priest) on Jun 01, 2005 at 05:22 UTC
    The #! combination of characters is known as the "hash-bang". I don't know why it is called bang.. I thought it was an exclamation mark myself, but I suspect an American thought it would be cute to give that character a shorter name.

    The #! characters are interpreted by linux/unix shells to mean "run whatever program follows, then pass the text on all subsequent lines to that program". It's a simple way of constructing scripts in any language, merely by defining which interpreter should be used on the first line of the script.

    As the previous poster attests, you don't need it in windows. That's because windows is different to linux in that it determines which program to run your file by the file extension (in most cases this will be .pl on your system). Alternatively you can run your perl scripts by typing "perl <filename>".

    There is one situation where you must provide the "hash-bang" on scripts in windows - this is in Apache for Win32, when executing perl CGI scripts. Apparently Apache looks for which interpreter to use based on the first line of any CGI scripts.

      I thought it was an exclamation mark myself, but I suspect an American thought it would be cute to give that character a shorter name.
      Most multisyllabic character names get shortened in various ways. According to the jargon file, "bang" originated at CMU.

      Dear monarch

      (...)I don't know why it is called bang.. I thought it was an exclamation mark myself(...)

      A few years back, email and news were sent from server to server using a protocol (and program) called UUCP (Unix-to-Unix-CoPy). This used phone lines to push packets of text files from machines that for the most part, were "connected" to the network intermitently, a few times a day.

      In those days, email addresses had to include the routing information, telling each system how to forward your message. An email address could have been something like ucbarpa!dino!usb!lem (If memory serves well). Each word in the address would be a host/system/site identifier, as known by the corresponding node. The ! would then instruct said node to "bang" or bounce the file to the next system in line.

      As you may easily guess, this system was very awkward (sp?) to use. Later it was entirely replaced with SMTP and NNTP for direct transmission of email and Usenet news from server to server, using the Internet as transport.

      During that transition, things were complex because provisions had to be made for rewriting the addresses to RFC-822 format (ie user@site) and for combining RFC-822 and UUCP in the same address, to deal with hybrid or incomplete connections (ie usb!lem@dino.conicit.ve). To this respect, Internet is so much simpler now.

      Google for UUCP for more information.

      Best regards

      -lem, but some call me fokat

Re: Clarify My doubt on #!/usr/local/bin/perl
by ikegami (Patriarch) on Jun 01, 2005 at 05:21 UTC
    And for your second question, the "!" differentiates shebang lines from comments.
Re: Clarify My doubt on #!/usr/local/bin/perl
by gellyfish (Monsignor) on Jun 01, 2005 at 08:44 UTC

    The shebang line, contrary to what has already been stated, is not used by the Unix shell but by the execve system call - as per the execve(2) manpage:

    execve() executes the program pointed to by filename. filename must be either a binary executable, or a script starting with a line of the form "#! interpreter arg". In the latter case, the interpreter must be a valid pathname for an executable which is not itself a script, which will be invoked as interpreter arg filename.
    Windows uses another mechanism based on the extension of the file to determine how to execute the file, however Perl does take note of the shebang line however the program was invoked: if there is a shebang line and it contains "perl" then it will be parsed to determine any additional command line switches that may need to be applied - if perl finds a shebang lime that doesn't contain "perl" then it will attempt to re-execute the script using the "interpreter" found there - thus on windows you can create a file with a shebang line #!notepad and run it with perl then "notepad" will be started with your "script" open for editing.

    /J\

      Still a bit off. The shebang line is actually interpreted by the kernel. When the kernel is told to run something, it checks the first few bytes of a file looking for a magic number to figure out how to execute something. The magic number for passing it to an interperter, such as the shell, is whatever the characters '#!' mean on that machine (depending on whether it's big-endian or little-endian).

      For example, the magic number for ELF-formatted files is whatever ^?ELF is (where ^? is ctrl-?, not literally ^ and ?). The kernel uses this to use the ELF loader to load and execute that file. Other formats may be supported as well, depending on the kernel and architecture, and, on Linux, whether you've compiled in support for other formats.

        Er, yes. But it is through the agency of the execve(3) system call that this happens - you can see the code for this (for example) in /arch/i386/kernel/process.c in the Linux source. Okay in Linux it is further complicated by the binfmt_misc stuff, but in general for all Unix based OS execve is a kernel function that is exposed via the syscall mechanism in the C standard library.

        /J\

Re: Clarify My doubt on #!/usr/local/bin/perl
by gawatkins (Monsignor) on Jun 01, 2005 at 14:09 UTC

    The only case in win32 where a #!c:\perl\bin\perl.exe is required would be CGI scripts that run through Apache. Otherwise it is only optional for switches and such.

    Greg W

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others musing on the Monastery: (6)
As of 2022-10-03 14:38 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    My preferred way to holiday/vacation is:











    Results (14 votes). Check out past polls.

    Notices?