Upon startup, perl looks for your script in one of the following places:
stdin script you
must explicitly specify a `-' for the script name.After locating your script, perl compiles it to an internal form. If the script is syntactically correct, it is executed.
Note: on first reading this section may not make much sense to you. It's here at the front for easy reference.
A single-character option may be combined with the following option, if any. This is particularly useful when invoking a script using the `#!' construct which only allows one argument. Example:
#!/usr/bin/perl -spi.bak # same as -s -p -i.bak ...
Options include:
find
which can print filenames terminated by the null character, you can say
this:
find . -name '*.bak' -print0 | perl -n0e unlinkThe special value `00' will cause Perl to slurp files in paragraph mode. The value `0777' will cause Perl to slurp files whole since there is no legal character with that value.
perl -ane 'print pop(@F), "\n";'is equivalent to
while (<>) {
@F = split(' ');
print pop(@F), "\n";
}
perl -p -i.bak -e "s/foo/bar/;" ...is the same as using the script:
#!/usr/bin/perl -pi.bak s/foo/bar/;which is equivalent to
#!/usr/bin/perl
while (<>) {
if ($ARGV ne $oldargv) {
rename($ARGV, $ARGV . '.bak');
open(ARGVOUT, ">$ARGV");
select(ARGVOUT);
$oldargv = $ARGV;
}
s/foo/bar/;
}
continue {
print; # this prints to original filename
}
select(STDOUT);
except that the `-i' form doesn't need to compare `$ARGV' to
`$oldargv' to know when the filename has changed. It does, however,
use `ARGVOUT' for the selected filehandle. Note that `STDOUT'
is restored as the default output filehandle after the loop.
You can use eof to locate the end of each input file, in case you
want to append to each file, or reset line numbering
(see section Input/Output, for an example).
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.
sed -n or awk:
while (<>) {
... # your script goes here
}
Note that the lines are not printed by default. See `-p'
option to have lines printed. Here is an efficient way to delete all
files older than a week:
[ before version 4.003 ]
find . -mtime +7 -print | perl -ne 'chop;unlink;'[ version 4.003 and beyond ]
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.
sed:
while (<>) {
... # your script goes here
} continue {
print;
}
Note that the lines are printed automatically. To suppress printing use
the `-n' switch. A `-p' overrides a `-n' switch.
if,
else or define.)
#!/usr/bin/perl -s
if ($xyz) { print "true\n"; }
#!/usr/bin/perl
eval "exec /usr/bin/perl -S $0 $*"
if $running_under_some_shell;
The system ignores the first line and feeds the script to
`/bin/sh', which proceeds to try to execute the perl script
as a shell script. The shell executes the second line as a normal shell
command, and thus starts up the perl interpreter. On some
systems `$0' doesn't always contain the full pathname, so the
`-S' tells perl to search for the script if necessary.
After perl locates the script, it parses the lines and ignores
them because the variable `$running_under_some_shell' is never
true. A better construct than `$*' would be `${1+"$@"}',
which handles embedded spaces and such in the filenames, but doesn't
work if the script is being interpreted by csh. In order to
start up sh rather than csh, some systems may have to
replace the `#!' line with a line containing just a colon, which
will be politely ignored by perl. Other systems can't control
that, and need a totally devious construct that will work under any of
csh, sh or perl, such as the following:
eval '(exit $?0)' && eval 'exec /usr/bin/perl -S $0 ${1+"$@"}'
& eval 'exec /usr/bin/perl -S $0 $argv:q'
if 0;
undump 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 are going to run your executable as a
set-id program then you should probably compile it using taintperl
rather than normal perl. If you want to execute a portion of your
script before dumping, use the dump operator instead. Note:
availability of undump is platform specific and may not be
available for a specific port of perl.