in reply to Understanding code.
Hi, basically the first three lines are executed/interpreted by 2-3 programs (at least under *nix): the shell, the perl executable, and Emacs.
The first line tells Emacs to switch into (c)perl-mode. It is ignored by the shell, the kernel, and the perl executable (#, not #!...).
The second line is interpreted by the shell first. It basically re-evaluates the call to your shell(!) script when called for the first time. Then the shell executes the evaluated string (e.g. perl -S yourscript.pl arg1 arg2 arg3) which is an explicit call to the perl executable. Here, the first call of the script stops because the perl executable (e.g. /usr/bin/perl) replaces the current shell (exec), so the shell never sees the third line and thus, it cannot complain about the syntactically incorrect third line.
Now, the same script is executed by the perl executable. The -S switch tells perl to search your environments $PATH variable for directories where it may find your script. Perl ignores the first comment and interprets the 2nd and 3rd line as a conditional statement which condition is always false. So, it does not execute it and goes happily along its way...as a Perl script/program.
Why? Because the call to perl (the executable) does not use an absolute path,
it is called with respect to your shells current $PATH environment. So you can avoid
the shebang-line like #!/usr/bin/perl which uses an absolute path. Furthermore, since the first line does not start with
#! the script is might be executed with your current shell (environment). See cdarke's comment for further details.
You might be interested in looking up the man pages for
env.
Update: Some clarifications added (s/perl program/perl executable/g, meaning e.g. /usr/bin/perl). List of programs added.
This response belongs to a SoPW-question that originally looked like:
# use perl -*-Perl-*- eval 'exec perl -S $0 ${1+"$@"}' if 0;
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Understanding code.
by cdarke (Prior) on Oct 27, 2008 at 13:48 UTC |