Perl expects any command-line options, also known as
switches or flags , to come first on the command line. The next item is
usually the name of the script, followed by any additional arguments
(often filenames) to be passed into the script. Some of these
additional arguments may be switches, but if so, they must be
processed by the script, since Perl gives up parsing switches as soon
as it sees either a non-switch item or the special --
switch that terminates switch
processing.
A single-character switch with no argument may be combined (bundled) with the switch that follows it, if any. For example:
#!/usr/bin/perl -spi.bak
is the same as:
#!/usr/bin/perl -s -p -i.bak
Perl recognizes the switches listed in Table 3-1.
Table 3-1. Perl switches
Switch | Function | |||
---|---|---|---|---|
-- | Terminates switch processing, even if the next argument starts with a minus. It has no other effect. | |||
-0[octnum] | Specifies the record separator ( | |||
-a | Turns on autosplit mode when used with
-n or -p. An
implicit | |||
-c | Causes Perl to check the syntax of the script and
exit without executing it. More or less equivalent to having
| |||
-d | Runs the script under the Perl debugger. See Chapter 6. | |||
-d:foo | Runs the script under the control of a debugging or tracing module installed in the Perl library as Devel::foo. For example, -d:DProf executes the script using the Devel::DProf profiler. See also the section on DProf in Chapter 6. | |||
-Dnumber -Dlist | Sets debugging flags. (This works only if
debugging was compiled into the version of Perl you are
running.) You may specify either a number that is the sum of
the bits you want, or a list of letters. To watch how Perl
executes your script, for instance, use
-D14 or -Dslt.
Another useful value is -D1024
(-Dx), which lists your compiled syntax
tree. And -D512
(-Dr) displays compiled regular
expressions. The numeric value of the flags is available
internally as the special variable | |||
Bit | Letter | Meaning | ||
1 | | Tokenizing and parsing | ||
2 | | Stack snapshots | ||
4 | | Label stack processing | ||
8 | | Trace execution | ||
16 | | Object method lookup | ||
32 | | String/numeric conversions | ||
64 | | Print preprocessor command for -P | ||
128 | | Memory allocation | ||
256 | | Format processing | ||
512 | | Regular expression processing | ||
1,024 | | Syntax tree dump | ||
2,048 | | Tainting checks | ||
4,096 | | Memory leaks (not supported anymore) | ||
8,192 | | Hash dump; usurps values | ||
16,384 | | Scratchpad allocation | ||
32,768 | | Cleaning up | ||
-e commandline | May be used to enter one or more lines of script. If -e is used, Perl does not look for the name of a script in the argument list. Multiple -e commands may be given to build up a multiline script. (Make sure to use semicolons where you would in a normal program.) | |||
-Fpattern | Specifies the pattern to split on if
-a is also in effect. The pattern may be
surrounded by //, | |||
-h | Prints a summary of Perl’s command-line options. | |||
-i[extension] | Specifies that files processed by the | |||
-I[directory] | Directories specified by -I
are prepended to | |||
-l[octnum] | Enables automatic line-end processing. This
switch has two effects. First, when it’s used with
-n or -p, it causes
the line terminator to be automatically perl -lpe 'substr($_, 80) = ""' | |||
-m[-]module -M[-]module -M[-]'module ...'
-[ -mmodule | Executes | |||
-Mmodule | Executes You can also say -m module=foo,bar or -Mmodule= foo,bar as a shortcut for -M’module qw(foo bar)‘. This avoids the need to use quotes when importing symbols. The actual code generated by -Mmodule=foo,bar is: use module split(/,/, q{foo,bar}) The = form removes the distinction between -m and -M. | |||
-n | Causes Perl to assume the following loop around your script, which makes it iterate over filename arguments: LINE: while (<>) { ... # Your script goes here By default, the lines are not printed. (See -p to have lines printed.) BEGIN and END blocks may be used to capture control before or after the implicit loop. | |||
-p | Causes Perl to assume the following loop around your script, which makes it iterate over filename arguments: LINE: while (<>) { ... # Your script goes here } continue { print; The lines are printed automatically. To suppress printing, use the -n switch. If both are specified, the -p switch overrides -n. BEGIN and END blocks may be used to capture control before or after the implicit loop. | |||
-P | Causes your script to run through the C
preprocessor before compilation by Perl. (Since both comments
and cpp directives begin with the
| |||
-s | Enables some rudimentary parsing of switches on
the command line after the script name but before any filename
arguments or the -- switch terminator.
Any switch found there is removed from | |||
-S | Makes Perl use the PATH environment variable to
search for the script (unless the name of the script starts
with a slash). Typically, this is used to emulate | |||
-t | Forces “taint” checks to be turned on, but
warning will be issued instead of fatal errors. Warnings can
be controlled with | |||
-T | Forces “taint” checks to be turned on. Ordinarily, these checks are done only when running setuid or setgid. It’s a good idea to turn them on explicitly for programs run on another user’s behalf, such as CGI programs. | |||
-u | Causes Perl to dump core after compiling your
script. You can take this core dump and turn it into an
executable file by using the undump
program (not supplied). This speeds startup at the expense of
some disk space (which you can minimize by stripping the
executable). If you want to execute a portion of your script
before dumping, use Perl’s | |||
-U | Allows Perl to do unsafe operations. Currently, the only “unsafe"operations are the unlinking of directories while running as superuser and running setuid programs with fatal taint checks turned into warnings. | |||
-v | Prints the version and patch level of your Perl executable. | |||
-V | Prints a summary of the major Perl configuration
values and the current value of | |||
-V:name | Prints the value of the named configuration variable to STDOUT. | |||
-w | Prints warnings about identifiers that are mentioned only once and scalar variables that are used before being set. Also warns about redefined subroutines and references to undefined filehandles or to filehandles opened as read-only that you are attempting to write on. Warns you if you use a non-number as though it was a number, if you use an array as though it was a scalar, if your subroutines recurse more than 100 levels deep, etc. | |||
-x[directory] | Tells Perl to extract a script that is embedded
in a message by looking for the first line that starts with
|
3.145.67.172