perl - Practical Extraction and Report Language
perl [-sTuU]
[-hv ] [-V[:configvar]]
[-cw ] [-d[:debugger]] [-D[number/list]]
[-pna ] [-Fpattern ] [-l[octal]] [-0[octal]]
[-Idir ] [-m[-]module ] [-M[-]'module...']
[-P]
[-S]
[-x[dir]]
[-i[extension]]
[-e 'command' ] [--] [programfile] [argument]...
For ease of access, the Perl manual has been divided into a number of sections. These can be viewed with the command perldoc(1).
If you intend to read these straight through for the first time, doing so in order in which the have been provided in this list can reduce the number of forward references.
perl | An overview of perl (this section) |
perltoc | Perl documentation table of contents |
perldata | Perl data structures |
perlsyn | Perl syntax |
perlop | Perl operators and precedence |
perlre | Perl regular expressions |
perlrun | Perl execution and options |
perlfunc | Perl built-in functions |
perlvar | Perl predefined variables |
perlsub | Perl subroutines |
perlmod | Perl modules |
perlref | Perl references |
perldsc | Perl data structures introduction |
perllol | Perl data structures: lists of lists |
perlobj | Perl objects |
perltie | Perl objects hidden behind simple variables |
perlbot | Perl OO tricks and examples |
perldebug | Perl debugging |
perldiag | Perl diagnostic messages |
perlform | Perl formats |
perlipc | Perl interprocess communication |
perlsec | Perl security |
perltrap | Perl traps for the unwary |
perlstyle | Perl style guide |
perlxs | Perl XS application programming interface |
perlxstut | Perl XS tutorial |
perlguts | Perl internal functions for those doing extensions |
perlcall | Perl calling conventions from C |
perlembed | Perl how to embed perl in your C or C++ application |
perlpod | Perl "plain old documentation" |
perlbook | Perl book information |
Additional documentation for Perl modules is available in the /usr/share/man/ directory. Some of this is distributed standard with Perl, but you will also find third-party modules there. You should be able to view this with the man(1) program by including the proper directories in the appropriate start-up files. To determine where these are, type:
perl -le 'use Config; print "@Config{man1dir,man3dir}"'
If the directories were /usr/local/man/cat1 and /usr/local/man/cat3, you need only add /usr/local/man to your MANPATH. If they are different, you must add both stems.
If that fails, you can still use the supplied perldoc script to view module information. You might also consider getting a replacement man(1) program.
If something has gone wrong with your program, and you are uncertain where you should look for help, try the -w switch first. It will often point out exactly where the trouble is.
Perl is an interpreted language optimized for scanning arbitrary text files, extracting information from those text files, and printing reports based on that information. It is also a good language for many system management tasks. The language is intended to be practical (easy-to-use, efficient, complete) rather than beautiful (tiny, elegant, minimal).
Upon startup, Perl looks for your script in one of the following places:
With methods 2 and 3, Perl starts parsing the input file from the beginning, unless you have specified a -x 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 script embedded in a larger message. (In this case, you would indicate the end of the script using the __END__ token.)
As of Perl 5, the #! line is always examined for switches as the line is being parsed. Thus, if you are on a computer that only allows one argument with the #! line, or does not recognize the #! line, you still can get consistent switch behavior regardless of how Perl was invoked, even if -x was used to find the beginning of the script.
Because many operating systems silently chop off kernel interpretation of the #! line after 32 characters, some switches might be passed in on the command line, and some might not. You could even get a "-" without its letter, if you are not careful. You should ensure that all of your switches fall either before or after that 32 character boundary. For most switches, it does not matter whether they are processed redundantly, but getting a - instead of a complete switch could cause Perl to try to execute standard input instead of your script. A partial -I switch could also cause odd results.
Parsing of the #! switches starts wherever "perl" is mentioned in the line. The sequences "-*" and "- " are specifically ignored so that you could say:
#!/bin/sh -- # -*- perl -*- -p
eval 'exec perl $0 -S ${1+"$@"}'
if 0;
to let Perl see the -p switch.
If the #! line does not contain the word "perl", the program named after the #! is executed instead of the Perl interpreter. This might seem unusual, but it helps those using computers that do not do #!, because they can tell a program that their SHELL is /usr/bin/perl, and Perl will then dispatch the program to the correct interpreter for them.
After locating your script, Perl compiles the entire script to an internal form. If any compilation errors occur, execution of the script is not attempted. (This is unlike the typical shell script, which might run part of the way through before finding a syntax error.)
If the script is syntactically correct, it is executed. If the
script runs off the end without hitting an exit() or
die() operator, an implicit exit(0)
is provided
to indicate successful completion.
A single-character switch can be combined with the following switch:
#!/usr/bin/perl -spi.bak # same as -s -p -i.bak
Switches include:
find . -name '*.bak' -print0 | perl -n0e unlink
The 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";
}
An alternate delimiter can be specified using -F.
1 | p | Tokenizing and parsing |
2 | s | Stack snapshots |
4 | l | Label stack processing |
8 | t | Trace execution |
16 | o | Operator node construction |
32 | c | String/numeric conversions |
64 | P | Print preprocessor command for -P |
128 | m | Memory allocation |
256 | f | Format processing |
512 | r | Regular expression parsing |
1024 | x | Syntax tree dump |
2048 | u | Tainting checks |
4096 | L | Memory leaks (no longer supported) |
8192 | H | Hash dump usurps values() |
16384 | X | Scratch-pad allocation |
32768 | D | Cleaning up |
$ perl -p -i.bak -e "s/cat/dog/; ... "
is the same as using the script:
#!/usr/bin/perl -pi.bak
s/cat/dog/;
which is equivalent to
#!/usr/bin/perl
while (<>) {
if ($ARGV ne $oldargv) {
rename($ARGV, $ARGV . '.bak');
open(ARGVOUT, ">$ARGV");
select(ARGVOUT);
$oldargv = $ARGV;
}
s/cat/dog/;
}
continue {
print; # this prints to original filename
}
select(STDOUT);
except that the -i form does not need to compare $ARGV to $oldargv to determine whether the file name has changed. It does, however, use ARGVOUT for the selected file handle. Note that STDOUT is restored as the default output file handle after the loop.
You can use eof without parentheses to locate the end of each input file if you want to append to each file or reset line numbering (see example in perlfunc/eof.
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.-mmodule executes use module; before executing your script. You can use quotes to add extra code after the module name, as in the example:
-M'module qw(cat dog)'
If the first character after the -M or -m is a dash (-), the use is replaced with no.
You can also use either -mmodule=cat,dog
or
-Mmodule=cat,dog
as a shortcut for
-M'module qw(cat dog)'
If you do this, it will not be necessary to use quotes when
importing symbols. The actual code generated by
-Mmodule=cat,dog
is use module
split(/,/,q{cat,dog})
Note that the = form removes
the distinction between -m and -M.
while (<>) {
... # your script goes here
}
Note that the lines are not printed by default. See -p to have lines printed. Here is an efficient way to delete all files older than a week:
find . -mtime +7 -print | perl -nle 'unlink;'
This is faster than using the -exec switch of find(1)
because it eliminates the need to start a process on every file
name found.
BEGIN and END blocks Can be used to capture control before or after the implicit loop, just as in awk(1).
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.
BEGIN and END blocks can be used to capture control before or after the implicit loop, just as in awk.
#!/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 then tries 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 does not always contain the full path name, 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 file names, but does not work if the script is being interpreted by csh. To start up sh rather than csh, some systems might have to replace the #! line with a line containing just a colon (:), which will be politely ignored by Perl. Other systems cannot control that, and require a construct that will work under csh, sh or Perl, such as the following example:
eval '(exit $?0)' && eval 'exec /usr/bin/perl -S $0 ${1+"$@"}'
& eval 'exec /usr/bin/perl -S $0 $argv:q'
if 0;
Perl combines some of the best features of C, sed(1), awk(1), and sh(1), so people familiar with those languages should have little difficulty with it. (Language historians will also note some vestiges of csh(1), Pascal, and even BASIC-PLUS.) Expression syntax corresponds quite closely to C expression syntax. Unlike most utilities, Perl does not arbitrarily limit the size of your data if you have got the memory, Perl can slurp in your whole file as a single string. Recursion is of unlimited depth. And the hash tables used by associative arrays grow as necessary to prevent degraded performance.
Perl uses sophisticated pattern matching techniques to scan large amounts of data very quickly. Although optimized for scanning text, Perl can also deal with binary data, and can make dbm files look like associative arrays. Setuid Perl scripts are safer than C programs through a data-flow tracing mechanism which prevents many stupid security holes. If you have a problem that would ordinarily use sed(1) or awk(1) or sh(1), but it exceeds their capabilities or must run a little faster, and you don't want to write the silly thing in C, then Perl may be for you. There are also translators to turn your sed(1) and awk(1) scripts into Perl scripts.
Perl version 5 is nearly a complete rewrite, and provides the following additional benefits:
It is now possible to write much more readable Perl code (even within regular expressions). Formerly cryptic variable names can be replaced by mnemonic identifiers. Error messages are more informative, and the optional warnings will catch many mistakes a novice might make. This cannot be stressed enough. Whenever you get unusual behavior, try using the -w switch.
The new yacc grammar is one half the size of the old one. Many of the arbitrary grammar rules have been standardized. The number of reserved words has been cut by two-thirds. Despite this, nearly all old Perl scripts will continue to work unchanged.
Perl variables can now be declared within a lexical scope, like "auto" variables in C. This is more efficient and provides better privacy for "programming in the large".
Any scalar value, including any array element, can now contain a reference to any other variable or subroutine. You can easily create anonymous variables and subroutines. Perl manages your reference counts for you.
The Perl library is now defined by modules that can be easily shared among various packages. A package can import all or a portion of a module's published interface. Pragmas (that is, compiler directives) are defined and used by the same mechanism.
A package can function as a class. Dynamic multiple inheritance and virtual methods are supported in a straightforward manner and with very little new syntax. File handles can now be treated as objects.
Perl can now be embedded easily in your C or C++ application, and can either call or be called by your routines through a documented interface. The XS preprocessor is provided to make it easy to glue your C or C++ routines into Perl. Dynamic loading of modules is supported.
A major new module is the POSIX module, which provides access to all available POSIX routines and definitions through object classes, where appropriate.
The new BEGIN and END blocks provide means to capture control as a package is being compiled, and after the program exits. As a degenerate case, they work just like awk's BEGIN and END when you use the -p or -n switches.
A Perl program can now access database management (DBM), name service database (NDBM), substitute database management (SDBM), GNU database management (GDBM), and Berkeley database (DB) files from the same script simultaneously. The old dbmopen interface has been generalized to allow any variable to be tied to an object class that defines its access methods.
In fact, the AUTOLOAD mechanism also allows you to define any arbitrary semantics for undefined subroutine calls. It is not just for autoloading.
You can now specify non-greedy quantifiers. You can now do grouping without creating a back reference. You can now write regular expressions with embedded white space and comments for readability. A consistent extensibility mechanism has been added that is upwardly compatible with all old regular expressions.
use lib "/my/directory";
BEGIN { require 'perl5db.pl' }
Apart from these, perl uses no other environment variables, except to make them available to the script being executed, and to child processes. However, scripts running setuid would do well to execute the following lines before doing anything else:
$ENV{'PATH'} = '/bin:/usr/bin'; # or whatever you need
$ENV{'SHELL'} = '/bin/sh' if defined $ENV{'SHELL'};
$ENV{'IFS'} = " if defined $ENV{'IFS'};
Larry Wall lwall@sems.com, with the help of oodles of other folks.
The -w switch produces some lovely diagnostics.
See perldiag for explanations of all Perl's diagnostics.
Compilation errors will tell you the line number of the error, with an indication of the next token or token type that was to be examined. (In the case of a script passed to Perl through -e switches, each -e is counted as one line.)
Setuid scripts have additional constraints that can produce error messages such as "Insecure dependency". See perlsec.
Again, it is strongly recommended that you consider using the -w switch.
The -w switch is not mandatory.
Perl depends on how your computer handles various operations, such as type casting, atof(3) and sprintf(3). The latter can even trigger a core dump when passed nonsensical input values.
If your stdio requires a seek or end-of-file (eof) between read operations and write operations on a particular stream, so does Perl. (This does not apply to sysread() and syswrite().)
While none of the built-in data types have any arbitrary size limits (apart from memory size), there are still a few arbitrary limits: a given identifier cannot be longer than 255 characters, and no component of your PATH can be longer than 255 if you use -S. A regular expression cannot compile to more than 32767 bytes internally.
See the perl bugs database at http://perl.com/perl/bugs/ You can mail your bug reports (be sure to include full configuration information as output by the myconfig program in the perl source tree) to perlbug@perl.com. If you have succeeded in compiling perl, the perlbug script in the utils/ subdirectory can be used to help mail in a bug report.