Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

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

by gellyfish (Monsignor)
on Jun 01, 2005 at 08:44 UTC ( [id://462344]=note: print w/replies, xml ) Need Help??


in reply to Clarify My doubt on #!/usr/local/bin/perl

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\

Replies are listed 'Best First'.
Re^2: Clarify My doubt on #!/usr/local/bin/perl
by Tanktalus (Canon) on Jun 01, 2005 at 13:59 UTC

    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\

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others goofing around in the Monastery: (4)
As of 2024-04-19 00:47 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found