perl

NAME

perl - Practical Extraction and Report Language

SYNOPSIS

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.

DESCRIPTION

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:

  1. Specified line-by-line through -e switches on the command line.
  2. Contained in the file specified by the first file name on the command line. (Note that systems supporting the #! notation invoke interpreters this way.)
  3. Passed in implicitly through standard input. This only works if there are no file-name arguments. To pass arguments to a STDIN script you must explicitly specify a "-" for the script name.

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.

SWITCHES

A single-character switch can be combined with the following switch:

#!/usr/bin/perl -spi.bak # same as -s -p -i.bak

Switches include:

-0[digits]
Specifies the record separator ($/) as an octal number. If there are no digits, the null character is the separator. Other switches can precede or follow the digits. For example, if you have a version of find(1) that can print file names terminated by the null character, you can use the following:
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.

-a
Turns on auto-split mode when used with a -n or -p. An implicit split command to the @F array is done as the first thing inside the implicit while loop produced by the -n or -p.
perl -ane 'print pop(@F), "\n";'
is equivalent to
while (<>) {
	@F = split(' ');
	print pop(@F), "\n";
}

An alternate delimiter can be specified using -F.

-c
Causes Perl to check the syntax of the script and then exit without executing it. Actually, it will execute BEGIN, END, and use blocks, because these are considered as occurring outside the execution of your program.
-d
Runs the script under the Perl debugger. See perldebug.
-d:cat
Runs the script under the control of a debugging or tracing module installed as Devel::cat. Thus, -d:DProf executes the script using the Devel::DProf profiler. See perldebug.
-Dnumber
-Dlist
Sets debugging flags. To see how it executes your script, use -D14. (This only works if debugging is compiled into your Perl.) Another nice value is -D1024, which lists your compiled syntax tree. Also, -D512 displays compiled regular expressions. As an alternative, specify a list of letters instead of numbers (for example, -D14 is equivalent to -Dtls):
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
-eCommand_Line
Can be used to enter one line of script. If -e is given, Perl will not look for a script file name in the argument list. Multiple -e commands can be given to build up a multiline script. Be 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 can be surrounded by //, "" or ". Otherwise, it will be placed within single quotes (').
-h
Prints a summary of the options.
-i[extension]
Specifies that files processed by the E construct are to be edited in place. It does this by renaming the input file, opening the output file by the original name, and selecting that output file as the default for print() statements. The extension, if supplied, is added to the name of the old file to make a back-up copy. If no extension is supplied, no back-up copy is made. From the shell, use:
$ 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.

-Idirectory
Directories specified by -I are prepended to the search path for modules (@INC), and also tells the C preprocessor where to search for include files. The C preprocessor is invoked with -P. By default it searches /usr/include and /usr/lib/perl.
-l[octnum]
Enables automatic line-ending processing. It has two effects: first, it automatically chomps the line terminator when used with -n or -p; second, it assigns $\ to have the value of octnum so that any print statements will have that line terminator added back on. If octnum is omitted, sets $\ to the current value of $/ . For instance, to trim lines to 80 columns, use:
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.
-m [-]module
-M [-]module
-M [-]'module ...'
-[mM] [-]module=arg[,arg]...
-mmodule executes use module(); before executing your script.

-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.
-n
Causes Perl to assume the following loop around your script, which makes it iterate over file-name arguments somewhat like sed -n or awk(1):
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).

-p
Causes Perl to assume the following loop around your script, which makes it iterate over file name arguments somewhat like sed(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.

-P
Causes your script to be run through the C preprocessor before compilation by Perl. (Because both comments and cpp directives begin with the number-sign character (#), you should avoid starting comments with any words recognized by the C preprocessor, such as "if", "else" or "define".)
-s
Enables some rudimentary switch parsing for switches on the command line after the script name but before any file-name arguments (or before a --). Any switch found there is removed from @ARGV and sets the corresponding variable in the Perl script. The following script prints "true" only if the script is invoked with a -xyz switch:
#!/usr/bin/perl -s
if ($xyz) { print "true\n"; }
-S
Makes Perl use the PATH environment variable to search for the script (unless the name of the script starts with a /). Typically, this is used to emulate #! startup on computers that don't support #!, in the following manner:
#!/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;
-T
Forces "taint" checks to be turned on so you can test them. Ordinarily, these checks are done only when running setuid or setgid. It is is recommended that you turn them on explicitly for programs run on another's behalf, such as Common Gateway Interface (CGI) programs. See perl perlsec.
-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). (Still, a "hello world" executable comes out to about 200 KB on some systems.) 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 might not be available for a specific port of Perl.
-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 summary of the major perl configuration values and the current value of @INC.
-V:name
Prints to STDOUT the value of the named configuration variable.
-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 file handles or file handles opened as read-only that you are attempting to write on. Also gives you warnings about other things, such as the following: you are using values as a number that does not look like numbers; you are using array as though it were a scalar; your subroutines recurse more than 100 deep. See perl perldiag and perl perltrap
-x directory
Tells Perl that the script is embedded in a message. Leading garbage will be discarded until the first line that starts with #! and contains the string "perl". Any meaningful switches on that line will be applied (but only one group of switches, as with normal #! processing). If a directory name is specified, Perl will switch to that directory before running the script. The -x switch only controls the disposal of leading garbage. The script must be terminated with __END__ if there is trailing garbage to be ignored (the script can process any or all of the trailing garbage by using the DATA file handle if desired).

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:

ENVIRONMENT

HOME
Used if chdir has no argument.
LOGDIR
Used if chdir has no argument and HOME is not set.
PATH
Used in executing subprocesses and in finding the script if -S is used.
PERL5LIB
A colon-separated list of directories in which to look for Perl library files before looking in the standard library and the current directory. If PERL5LIB is not defined, PERLLIB is used. When running taint checks (because the script was running setuid or setgid, or the -T switch was used), neither variable is used. The script should instead say
use lib "/my/directory";
PERL5DB
The command used to get the debugger code. If unset, uses
BEGIN { require 'perl5db.pl' }
PERLLIB
A colon-separated list of directories in which to look for Perl library files before looking in the standard library and the current directory. If PERL5LIB is defined, PERLLIB is not used.

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'};

AUTHOR

Larry Wall lwall@sems.com, with the help of oodles of other folks.

FILES

/tmp/perl-e$$
Temporary file for -e commands
@INC
Locations of perl 5 libraries

SEE ALSO

a2p(1)
awk(1) to perl(1) translator
s2p(1)
sed(1) to perl(1) translator

DIAGNOSTICS

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.

BUGS

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.