<P>The normal way to run a Perl program is by making it directly
executable, or else by passing the name of the source file as an
argument on the command line. (An interactive Perl environment
is also possible--see <A HREF="../../lib/Pod/perldebug.html">the perldebug manpage</A> for details on how to do that.)
Upon startup, Perl looks for your program in one of the following
places:</P>
<OL>
<LI>
Specified line by line via <STRONG>-e</STRONG> switches on the command line.
<P></P>
<LI>
Contained in the file specified by the first filename on the command line.
(Note that systems supporting the #! notation invoke interpreters this
way. See <A HREF="#location of perl">Location of Perl</A>.)
<P></P>
<LI>
Passed in implicitly via standard input. This works only if there are
no filename arguments--to pass arguments to a STDIN-read program you
must explicitly specify a ``-'' for the program name.
<P></P></OL>
<P>With methods 2 and 3, Perl starts parsing the input file from the
beginning, unless you've specified a <STRONG>-x</STRONG> switch, in which case it
scans for the first line starting with #! and containing the word
``perl'', and starts there instead. This is useful for running a program
embedded in a larger message. (In this case you would indicate the end
of the program using the <CODE>__END__</CODE> token.)</P>
<P>The #! line is always examined for switches as the line is being
parsed. Thus, if you're on a machine that allows only one argument
with the #! line, or worse, doesn't even recognize the #! line, you
still can get consistent switch behavior regardless of how Perl was
invoked, even if <STRONG>-x</STRONG> was used to find the beginning of the program.</P>
<P>Because historically some operating systems silently chopped off
kernel interpretation of the #! line after 32 characters, some
switches may be passed in on the command line, and some may not;
you could even get a ``-'' without its letter, if you're not careful.
You probably want to make sure that all your switches fall either
before or after that 32-character boundary. Most switches don't
actually care if they're processed redundantly, but getting a ``-''
instead of a complete switch could cause Perl to try to execute
standard input instead of your program. And a partial <STRONG>-I</STRONG> switch
could also cause odd results.</P>
<P>Some switches do care if they are processed twice, for instance
combinations of <STRONG>-l</STRONG> and <STRONG>-0</STRONG>. Either put all the switches after
the 32-character boundary (if applicable), or replace the use of
<STRONG>-0</STRONG><EM>digits</EM> by <CODE>BEGIN{ $/ = "\0digits"; }</CODE>.</P>
<P>Parsing of the #! switches starts wherever ``perl'' is mentioned in the line.
The sequences ``-*'' and ``- '' are specifically ignored so that you could,
if you were so inclined, say</P>
<PRE>
#!/bin/sh -- # -*- perl -*- -p
eval 'exec perl -wS $0 ${1+"$@"}'
if $running_under_some_shell;</PRE>
<P>to let Perl see the <STRONG>-p</STRONG> switch.</P>
<P>A similar trick involves the <STRONG>env</STRONG> program, if you have it.</P>
<PRE>
#!/usr/bin/env perl</PRE>
<P>The examples above use a relative path to the perl interpreter,
getting whatever version is first in the user's path. If you want
a specific version of Perl, say, perl5.005_57, you should place
that directly in the #! line's path.</P>
<P>If the #! line does not contain the word ``perl'', the program named after
the #! is executed instead of the Perl interpreter. This is slightly
bizarre, but it helps people on machines that don't do #!, because they
can tell a program that their SHELL is <EM>/usr/bin/perl</EM>, and Perl will then
dispatch the program to the correct interpreter for them.</P>
<P>After locating your program, Perl compiles the entire program to an
internal form. If there are any compilation errors, execution of the
program is not attempted. (This is unlike the typical shell script,
which might run part-way through before finding a syntax error.)</P>
<P>If the program is syntactically correct, it is executed. If the program
runs off the end without hitting an <A HREF="../../lib/Pod/perlfunc.html#item_exit"><CODE>exit()</CODE></A> or <A HREF="../../lib/Pod/perlfunc.html#item_die"><CODE>die()</CODE></A> operator, an implicit
<A HREF="../../lib/Pod/perlfunc.html#item_exit"><CODE>exit(0)</CODE></A> is provided to indicate successful completion.</P>
<P>
<H2><A NAME="#! and quoting on nonunix systems">#! and quoting on non-Unix systems</A></H2>
<P>Unix's #! technique can be simulated on other systems:</P>
<P>You can use <A HREF="../../lib/Pod/perlfunc.html#item_eof"><CODE>eof</CODE></A> without parentheses to locate the end of each input
file, in case you want to append to each file, or reset line numbering
(see example in <A HREF="../../lib/Pod/perlfunc.html#eof">eof in the perlfunc manpage</A>).</P>
<P>If, for a given file, Perl is unable to create the backup file as
specified in the extension then it will skip that file and continue on
with the next one (if it exists).</P>
<P>For a discussion of issues surrounding file permissions and <STRONG>-i</STRONG>,
see <A HREF="../../lib/Pod/perlfaq5.html#why does perl let me delete readonly files why does i clobber protected files isn't this a bug in perl">Why does Perl let me delete read-only files? Why does -i clobber protected files? Isn't this a bug in Perl? in the perlfaq5 manpage</A>.</P>
<P>You cannot use <STRONG>-i</STRONG> to create directories or to strip extensions from
files.</P>
<P>Perl does not expand <CODE>~</CODE> in filenames, which is good, since some
<STRONG>-m</STRONG><EM>module</EM> executes <A HREF="../../lib/Pod/perlfunc.html#item_use"><CODE>use</CODE></A> <EM>module</EM> <CODE>();</CODE> before executing your
program.
<P><STRONG>-M</STRONG><EM>module</EM> executes <A HREF="../../lib/Pod/perlfunc.html#item_use"><CODE>use</CODE></A> <EM>module</EM> <CODE>;</CODE> before executing your
program. You can use quotes to add extra code after the module name,
This obsolete switch causes Perl to dump core after compiling your
program. You can then in theory take this core dump and turn it
into an executable file by using the <STRONG>undump</STRONG> program (not supplied).
This speeds startup at the expense of some disk space (which you
can minimize by stripping the executable). (Still, a ``hello world''
executable comes out to about 200K on my machine.) If you want to
execute a portion of your program before dumping, use the <A HREF="../../lib/Pod/perlfunc.html#item_dump"><CODE>dump()</CODE></A>
operator instead. Note: availability of <STRONG>undump</STRONG> is platform
specific and may not be available for a specific port of Perl.
<P>This switch has been superseded in favor of the new Perl code
generator backends to the compiler. See <A HREF="../../lib/B.html">the B manpage</A> and <A HREF="../../lib/B/Bytecode.html">the B::Bytecode manpage</A>
prints warnings about dubious constructs, such as variable names
that are mentioned only once and scalar variables that are used
before being set, redefined subroutines, references to undefined
filehandles or filehandles opened read-only that you are attempting
to write on, values used as a number that doesn't look like numbers,
using an array as though it were a scalar, if your subroutines
recurse more than 100 deep, and innumerable other things.
<P>This switch really just enables the internal <CODE>^$W</CODE> variable. You
can disable or promote into fatal errors specific warnings using
<CODE>__WARN__</CODE> hooks, as described in <A HREF="../../lib/Pod/perlvar.html">the perlvar manpage</A> and <A HREF="../../lib/Pod/perlfunc.html#warn">warn in the perlfunc manpage</A>.
See also <A HREF="../../lib/Pod/perldiag.html">the perldiag manpage</A> and <A HREF="../../lib/Pod/perltrap.html">the perltrap manpage</A>. A new, fine-grained warning
facility is also available if you want to manipulate entire classes
of warnings; see <A HREF="../../lib/warnings.html">the warnings manpage</A> or <A HREF="../../lib/Pod/perllexwarn.html">the perllexwarn manpage</A>.</P>