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 .
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 then exit without executing it. More or less equivalent to having |
||
-d |
Runs the script under the Perl debugger. See Chapter 6, Debugging . |
||
-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 |
Sets debugging flags. (This only works 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 |
||
-Dlist |
|||
Bit | Letter | Meaning | |
1 |
p
|
Tokenizing and parsing | |
2 |
s
|
Stack snapshots | |
4 |
l
|
Label stack processing | |
8 |
t
|
Trace execution | |
16 |
o
|
Object method lookup | |
32 |
c
|
String/numeric conversions | |
64 |
P
|
Print preprocessor command for |
|
128 |
m
|
Memory allocation | |
256 |
f
|
Format processing | |
512 |
r
|
Regular expression processing | |
1,024 |
x
|
Syntax tree dump | |
2,048 |
u
|
Tainting checks | |
4,096 |
L
|
Memory leaks (not supported any more) | |
8,192 |
H
|
Hash dump - usurps values |
|
16,384 |
X
|
Scratchpad allocation | |
32,768 |
D
|
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 |
||
-Idirectory |
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 |
Executes |
||
-M [ - ] module | |||
-M [ - ] 'module ...' | |||
-[
mM
][
-
]
module
arg
[
,arg
]
...
|
|||
-m module | |||
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: The = form removes the distinction between -m and -M .use module split(/,/, q{foo,bar}) |
|||
-n |
Causes Perl to assume the following loop around your script, which makes it iterate over filename arguments: 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.LINE: while (<>) { ... # your script goes here |
||
-p |
Causes Perl to assume the following loop around your script, which makes it iterate over filename arguments: 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.LINE: while (<>) { ... # your script goes here } continue { print; |
||
Causes your script to be 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 |
||
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 then 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 | |||
-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 were a number, if you use an array as though it were 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 |
Copyright © 2001 O'Reilly & Associates. All rights reserved.