how to execute the Perl interpreter
|
-m[-]module
-M[-]module
-M[-]'module ...'
-[mM][-]module=arg[,arg]...
-mmodule executes "use" module "();" before executing your program.
-Mmodule executes "use" module ";" before executing your program. You can use quotes to add extra
code after the module name, e.g., '-MMODULE qw(foo bar)'.
If the first character after the -M or -m is a dash (-) then the 'use' is replaced with 'no'.
A little builtin syntactic sugar means you can also say -mMODULE=foo,bar or -MMODULE=foo,bar as a
shortcut for '-MMODULE 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})". Note that the
"=" form removes the distinction between -m and -M.
A consequence of this is that -MMODULE=number never does a version check, unless "MODULE::import()"
itself is set up to do a version check, which could happen for example if MODULE inherits from
Exporter.
|
-0[octal/hexadecimal]
specifies the input record separator ($/) as an octal or hexadecimal number. If there are no
digits, the null character is the separator. Other switches may precede or follow the digits. For
example, if you have a version of find which can print filenames terminated by the null character,
you can say this:
find . -name '*.orig' -print0 | perl -n0e unlink
The special value 00 will cause Perl to slurp files in paragraph mode. Any value 0400 or above will
cause Perl to slurp files whole, but by convention the value 0777 is the one normally used for this
purpose.
You can also specify the separator character using hexadecimal notation: -0xHHH..., where the "H"
are valid hexadecimal digits. Unlike the octal form, this one may be used to specify any Unicode
character, even those beyond 0xFF. So if you really want a record separator of 0777, specify it as
-0x1FF. (This means that you cannot use the -x option with a directory name that consists of
hexadecimal digits, or else Perl will think you have specified a hex number to -0.)
|
-n causes Perl to assume the following loop around your program, which makes it iterate over filename
arguments somewhat like sed -n or awk:
LINE:
while (<>) {
... # your program goes here
}
Note that the lines are not printed by default. See -p to have lines printed. If a file named by
an argument cannot be opened for some reason, Perl warns you about it and moves on to the next file.
Also note that "<>" passes command line arguments to "open" in perlfunc, which doesn't necessarily
interpret them as file names. See perlop for possible security implications.
Here is an efficient way to delete all files that haven't been modified for at least a week:
find . -mtime +7 -print | perl -nle unlink
This is faster than using the -exec switch of find because you don't have to start a process on
every filename found. It does suffer from the bug of mishandling newlines in pathnames, which you
can fix if you follow the example under -0.
"BEGIN" and "END" blocks may be used to capture control before or after the implicit program loop,
just as in awk.
|
-l[octnum]
enables automatic line-ending processing. It has two separate effects. First, it automatically
chomps $/ (the input record separator) when used with -n or -p. Second, it assigns "$\" (the output
record separator) to have the value of octnum so that any print statements will have that separator
added back on. If octnum is omitted, sets "$\" to the current value of $/. For instance, to trim
lines to 80 columns:
perl -lpe 'substr($_, 80) = ""'
Note that the assignment "$\ = $/" is done when the switch is processed, so the input record
separator can be different than the output record separator if the -l switch is followed by a -0
switch:
gnufind / -print0 | perl -ln0e 'print "found $_" if -p'
This sets "$\" to newline and then sets $/ to the null character.
|
-e commandline
may be used to enter one line of program. If -e is given, Perl will not look for a filename in the
argument list. Multiple -e commands may be given to build up a multi-line script. Make sure to use
semicolons where you would in a normal program.
|