-d: Runs the script under the debugger. E.g. perl -d myscript.pl
-e: May be used to enter a single line of script.
-w: Prints warnings about possible spelling errors and other error-prone constructs in the script.
123
123.4
5E-10
0xff (hex)
0377 (octal)
Literal string, no variable interpolation or escape characters, except \' and \\.
Variables are interpolated and escape sequences are processed. E.g.
$x = 5; print "The value of variable x is $x";
Escape sequences: \t (Tab), \n (Newline), \r (Return),\f (Formfeed).
\l and \u lowercase/uppercase the following character. \L and \U lowercase/uppercase until a \E is encountered.
Evaluates to the output of the command.
(1, 2, 3)
( ) is an empty array.
(1..4) is the same as (1,2,3,4), likewise ('a'..'z').
sub { statements }
STDIN, STDOUT, STDERR, ARGV.
User-specified: handle, $var.
<<identifier
...
identifier
Shell-style "here document."
A simple scalar variable.
29th element of array @var.
Last element of array @var.
$j-th element of $i-th element of array @var.
A value from hash (associative array) %var.
Last index of array @var.
The entire array; in a scalar context, the number of elements in the array.
A slice of array @var.
A slice of %var; same as ($var{'a'},$var{'b'}).
The entire hash; in a scalar context, true if the hash has elements.
** |
Exponentiation |
|||
+ | - | * | / |
Addition, subtraction, multiplication, division |
% |
Modulo division |
|||
& | | | ^ |
Bitwise AND, bitwise OR, bitwise exclusive OR |
|
>> | << |
Bitwise shift right, bitwise shift left |
||
|| | && |
Logical OR, logical AND |
||
. |
Concatenation of two strings |
All of the above operators have an associated assignment operator, e.g., .=
! | ~ |
Negation (unary), bitwise complement (unary) |
++ | -- |
Auto-increment (magical on strings), auto-decrement |
== | != |
Numeric equality, inequality |
eq | ne | String equality, inequality |
< | > |
Numeric less than, greater than |
lt | gt | String less than, greater than |
<= | >= |
Numeric less (greater) than or equal to |
le | ge |
String less (greater) than or equal to |
<=> | cmp |
Numeric (string) compare. Returns -1, 0, or 1. |
=~ | !~ |
Search pattern, substitution, or translation (negated) |
.. |
Range (scalar context) or enumeration (array context) |
|
? : |
Alternation (if-then-else) operator |
|
, |
Comma operator, also list element separator. |
|
not |
A "list" is a list of expressions, variables, or lists. An array variable or an array slice may always be used instead of a list.
Parentheses can be added around the parameter lists to avoid precedence problems.
Every statement is an expression, optionally followed by a modifier, and terminated with a semicolon.
Execution of expressions can depend on other expressions using one of the modifiers if, unless, while, or until, for example:
expr1 if expr2 ; expr1 until expr2 ;
The logical operators ||, &&, or ?: also allow conditional execution:
expr1 || expr2 ; expr1 ? expr2 : expr3 ;
Statements can be combined to form a block when enclosed in {}. blocks may be used to control flow:
if (expr) block [ [ elsif (expr) block...] else block ]
E.g.
if ($x > 1) { $x++;} elsif ($x > 0) { $x = 0;} else { $x--;}
unless (expr) block [ else block ]
while (expr) block
until (expr) block
for ( [ expr ] ; [ expr ] ; [ expr ] ) block
E.g.
@table = ('red', 'blue', 'green', 'black', 'yellow'); for ($i=0; $i < @table; $i++) { print "The value is $table[$i].\n" }
foreach var (list) block
E.g.
foreach $color (@table) { print "The value is $color.\n" }
Use of "each" with hash arrays (assume there is a hash array "myarray"):
while (($holder, $record) = each(%myarray)) { print "The value in location $holder is: $record.\n"; }
Special forms are:
do block while expr ; do block until expr ;
which are guaranteed to perform block once before testing expr, and
do block
which effectively turns block into an expression.
Executes a subroutine declared by a sub declaration, and returns the value of the last expression evaluated in subroutine.
Imports the named subroutines from module.
If expr is numeric, requires Perl to be at least that version. Otherwise expr must be the name of a file that is included from the Perl library. Does not include more than once, and yields a fatal error if the file does not evaluate to a true value. If expr is a bare word, assumes extension .pm for the name of the file.
Returns from a subroutine with the value specified.
Designates name as a subroutine. Parameters are passed by reference as array @_. Returns the value of the last expression evaluated.
Returns the absolute value of its operand.
Returns the arctangent of y/x in the range -pi to pi.
Returns the cosine of expr (expressed in radians).
Returns e to the power of expr.
Returns the integer portion of expr.
Returns natural logarithm (base e) of expr.
Returns a random fractional number between 0 and the value of expr. If expr is omitted, returns a value between 0 and 1.
Returns the sine of expr (expressed in radians).
Returns the square root of expr.
Sets the random number seed for the rand operator.
Returns the number of seconds since January 1, 1970. Suitable for feeding to gmtime and localtime.
Returns the character represented by the decimal value expr.
Converts a time as returned by the time function to a 9-element array (0:$sec, 1:$min, 2:$hour, 3:$mday, 4:$mon, 5:$year, 6:$wday, 7:$yday, 8:$isdst) with the time analyzed for the Greenwich time zone. $mon has the range 0..11 and $wday has the range 0..6.
Returns the decimal value of expr interpreted as a hex string.
Converts a time as returned by the time function to ctime(3) string. In array context, returns a 9-element array with the time analyzed for the local time zone.
Returns the decimal value of expr interpreted as an octal string. If expr starts off with 0x, interprets it as a hex string instead.
Returns the ASCII value of the first character of expr.
Treats string expr as a vector of unsigned integers, and yields the bit at offset. bits must be between 1 and 32. May have a value assigned to it.
Removes line endings from all elements of the list; returns the (total) number of characters removed.
Chops off the last character on all elements of the list; returns the last chopped character.
expr is parsed and executed as if it were a Perl program. The value returned is the value of the last expression evaluated. If there is a syntax error or runtime error, an undefined string is returned by eval, and $@ is set to the error message.
Returns the length in characters of the value of expr.
Returns a lowercase version of expr.
Returns expr with the first character lowercase.
Extracts a substring of length len out of expr and returns it. If offset is negative, counts from end of the string. May have a value assigned to it.
Returns an uppercased version of expr.
Returns expr with the first character uppercased.
Deletes the specified value from the specified hash. Returns the deleted value (unless hash is tied to a package that does not support this).
Returns a 2-element array consisting of the key and value for the next value of the hash. Entries are returned in an apparently random order. After all values of the hash have been returned, a null array is returned. The next call to each after that will start iterating again.
Checks if the specified hash key exists in its hash array.
Joins the separate strings of list into a single string with fields separated by the value of expr, and returns the string.
Returns an array of all the keys of the named hash.
Pops off and returns the last value of the array.
Pushes the values of list onto the end of the array.
In array context, returns the list in reverse order. In scalar context, returns the first element of list with bytes reversed.
Returns the number of elements in the array.
Returns a true value if the hash has elements defined.
Shifts the first value of the array off and returns it, shortening the array by 1 and moving everything down. If @array is omitted, shifts @ARGV in main and @_ in subroutines.
Sorts the list and returns the sorted array value. subroutine, if specified, must return less than zero, zero, or greater than zero, depending on how the elements of the array (available to the routine as $a and $b) are to be ordered. subroutine may be the name of a user-defined routine, or a block.
Removes the elements of @array designated by offset and length, and replaces them with list (if specified). Returns the elements removed.
Splits a string into an array of strings, and returns it. If limit is specified, splits into at most that number of fields. If pattern is also omitted, splits at the whitespace. If not in array context, returns number of fields and splits to @_.
Prepends list to the front of the array, and returns the number of elements in the new array.
Returns a normal array consisting of all the values of the named hash.
Each character matches itself, unless it is one of the special characters + ? . * ^ $ ( ) [ ] { } | \. The special meaning of these characters can be escaped using a \.
Matches an arbitrary character, but not a newline unless it is a single-line match (see m/ /s).
Groups a series of pattern elements to a single element.
Matches the beginning of the target. In multiline mode (see m//m) also matches after every newline character.
Matches the end of the line. In multiline mode also matches before every newline character.
Denotes a class of characters to match. [^] negates the class.
Matches one of the alternatives.
Matches the preceding pattern element one or more times.
Matches zero or one times.
Matches zero or more times.
Searches expr (default: $_) for a pattern. If you prepend an m you can use almost any pair of delimiters instead of the slashes. If used in array context, an array is returned consisting of the subexpressions matched by the parentheses in the pattern, i.e., ($1, $2, $3,...). Optional modifiers: g matches as many times as possible; i searches in a case-insensitive manner; o interpolates variables only once. m treats the string as multiple lines; s treats the string as a single line; x allows for regular expression extensions. If pattern is empty, the most recent pattern from a previous match or replacement is used. With g the match can be used as an iterator in scalar context.
This is just like the /pattern/ search, except that it matches only once between calls to the reset operator.
Searches a string for a pattern, and if found, replaces that pattern with the replacement text. It returns the number of substitutions made, if any; if no substitutions are made, it returns false. Optional modifiers: g replaces all occurrences of the pattern; e evaluates the replacement string as a Perl expression; for the other modifiers, see /pattern/ matching. Almost any delimiter may replace the slashes; if single quotes are used, no interpolation is done on the strings between the delimiters, otherwise the strings are interpolated as if inside double quotes. If bracketing delimiters are used, pattern and replacement may have their own delimiters, e.g., s(foo)[bar]. If pattern is empty, the most recent pattern from a previous match or replacement is used.
Translates all occurrences of the characters found in the search list with the corresponding character in the replacement list. It returns the number of characters replaced. y may be used instead of tr. Optional modifiers: c complements the searchlist; d deletes all characters found in searchlist that do not have a corresponding character in replacementlist; s squeezes all sequences of characters that are translated into the same target character into one occurrence of this character.
Returns the position where the last m/ /g search left off for scalar. May have a value assigned to it.
Studies the scalar variable $var in anticipation of performing many pattern matches on its contents before the variable is next modified.
In input/output operations, filehandle may be a filehandle as opened by the open operator, a predefined filehandle (e.g., STDOUT), or a scalar variable that evaluates to the name of a filehandle to be used.
In scalar context, reads a single line from the file opened on filehandle. In array context, reads the whole file.
Reads from the input stream formed by the files specified in @ARGV, or standard input if no arguments were supplied.
Arranges for the file opened on filehandle to be read or written in binary mode as opposed to text mode (null-operation on UNIX).
Closes the file or pipe associated with the filehandle.
Returns true if the next read will return end of file, or if the file is not open.
Returns the EOF status for the last file read.
Opens a file and associates it with filehandle. If filename is omitted, the scalar variable of the same name as the filehandle must contain the filename.
The following filename conventions apply when opening a file:
Open file for input. Also "<file".
Open file for output, creating it if necessary.
Open file in append mode.
Open file with read/write access.
Prints the elements of list, converting them to strings if needed. If filehandle is omitted, prints by default to standard output (or to the last selected output channel, see select).
Equivalent to print filehandle and sprintf list.
Reads length binary bytes from the file into the variable at offset. Returns number of bytes actually read.
Arbitrarily positions the file. Returns true if successful.
Returns the currently selected filehandle. Sets the current default filehandle for output operations if filehandle is supplied.
Returns the current file position for the file. If filehandle is omitted, assumes the file last read.
Tests whether the lvalue expr has an actual value.
Executes filename as a Perl script.
Executes the code between { and }. Traps runtime errors as described with eval(expr), in "String Functions".
Creates a scope for the listed variables local to the enclosing block, subroutine, or eval.
Forces evaluation of expr in scalar context.
Undefines the lvalue. Always returns the undefined value.
The following variables are global and should be localized in subroutines:
The default input and pattern-searching space.
The current input line number of the last filehandle that was read.
The name of the current file when reading from < >.
The following variables are always local to the current block:
The string matched by the last successful pattern match.
The string preceding what was matched by the last successful match.
The string following what was matched by the last successful match.
The last bracket matched by the last search pattern.
Contain the subpatterns from the corresponding sets of parentheses in the last pattern successfully matched. $10... and up are only available if the match contained that many subpatterns.
Contains the command-line arguments for the script (not including the command name).
Parameter array for subroutines. Also used by split if not in array context.
Contains the current environment.