Uusmediasovellusten tekniikat, Quick Perl Reference

Some command-line options



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').

Code reference

sub { statements }



User-specified: handle, $var.



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.

@var[3,4,5], @var[3..5]

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.




+ - * /

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.


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 ]


   if ($x > 1) {
   elsif ($x > 0) {
     $x = 0;}
   else {

unless (expr) block [ else block ]

while (expr) block

until (expr) block

for ( [ expr ] ; [ expr ] ; [ expr ] ) block


   @table = ('red', 'blue', 'green', 'black', 'yellow');
   for ($i=0; $i < @table; $i++) {
      print "The value is $table[$i].\n"

foreach var (list) block


   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.

Subroutines, Packages, and Modules

&subroutine list

Executes a subroutine declared by a sub declaration, and returns the value of the last expression evaluated in subroutine.

import module [ [ version ] list ]

Imports the named subroutines from module.

require expr

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.

return expr

Returns from a subroutine with the value specified.

sub name { expr ;...}

Designates name as a subroutine. Parameters are passed by reference as array @_. Returns the value of the last expression evaluated.

Arithmetic Functions

abs expr

Returns the absolute value of its operand.

atan2 y, x

Returns the arctangent of y/x in the range -pi to pi.

cos expr

Returns the cosine of expr (expressed in radians).

exp expr

Returns e to the power of expr.

int expr

Returns the integer portion of expr.

log expr

Returns natural logarithm (base e) of expr.

rand [ expr ]

Returns a random fractional number between 0 and the value of expr. If expr is omitted, returns a value between 0 and 1.

sin expr

Returns the sine of expr (expressed in radians).

sqrt expr

Returns the square root of expr.

srand [ 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.

Conversion Functions

chr expr

Returns the character represented by the decimal value expr.

gmtime 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.

hex expr

Returns the decimal value of expr interpreted as a hex string.

localtime expr

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.

oct expr

Returns the decimal value of expr interpreted as an octal string. If expr starts off with 0x, interprets it as a hex string instead.

ord expr

Returns the ASCII value of the first character of expr.

vec expr, offset, bits

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.

String Functions

chomp list

Removes line endings from all elements of the list; returns the (total) number of characters removed.

chop list

Chops off the last character on all elements of the list; returns the last chopped character.

eval expr

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.

length expr(dagger)

Returns the length in characters of the value of expr.

lc expr

Returns a lowercase version of expr.

lcfirst expr

Returns expr with the first character lowercase.

substr expr, offset [ , len ]

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.

uc expr

Returns an uppercased version of expr.

ucfirst expr

Returns expr with the first character uppercased.

Array and List Functions

delete $hash{key}

Deletes the specified value from the specified hash. Returns the deleted value (unless hash is tied to a package that does not support this).

each %hash

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.

exists expr

Checks if the specified hash key exists in its hash array.

join expr, list

Joins the separate strings of list into a single string with fields separated by the value of expr, and returns the string.

keys %hash

Returns an array of all the keys of the named hash.

pop @array

Pops off and returns the last value of the array.

push @array, list

Pushes the values of list onto the end of the array.

reverse list

In array context, returns the list in reverse order. In scalar context, returns the first element of list with bytes reversed.

scalar @array

Returns the number of elements in the array.

scalar %hash

Returns a true value if the hash has elements defined.

shift [ @array ]

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.

sort [ subroutine ] list

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.

splice @array, offset [, length [ , list ] ]

Removes the elements of @array designated by offset and length, and replaces them with list (if specified). Returns the elements removed.

split [ pattern [ , expr [ , limit ] ] ]

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 @_.

unshift @array, list

Prepends list to the front of the array, and returns the number of elements in the new array.

values %hash

Returns a normal array consisting of all the values of the named hash.

Regular Expressions

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.

Search and Replace Functions

[ expr =~ ] [ m ] /pattern/ [g ] [ i ] [ m ] [ o ] [ s ] [\x\ ]

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.

[ $var =~ ] s/pattern/replacement/ [e] [g] [i] [m] [o] [s] [x]

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.

pos scalar

Returns the position where the last m/ /g search left off for scalar. May have a value assigned to it.

study [ $var(dagger) ]

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.

binmode filehandle

Arranges for the file opened on filehandle to be read or written in binary mode as opposed to text mode (null-operation on UNIX).

close filehandle

Closes the file or pipe associated with the filehandle.

eof 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.

open filehandle [ , filename ]

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.

print [ filehandle ] [ list]

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).

printf [ filehandle ] [ list ]

Equivalent to print filehandle and sprintf list.

read filehandle, $var, length [ , offset ]

Reads length binary bytes from the file into the variable at offset. Returns number of bytes actually read.

seek filehandle, position, whence

Arbitrarily positions the file. Returns true if successful.

select [ filehandle ]

Returns the currently selected filehandle. Sets the current default filehandle for output operations if filehandle is supplied.

tell [ filehandle ]

Returns the current file position for the file. If filehandle is omitted, assumes the file last read.


defined expr

Tests whether the lvalue expr has an actual value.

do filename

Executes filename as a Perl script.

eval { expr;... }

Executes the code between { and }. Traps runtime errors as described with eval(expr), in "String Functions".

local variable

local ( list )

Creates a scope for the listed variables local to the enclosing block, subroutine, or eval.

scalar expr

Forces evaluation of expr in scalar context.

undef [ lvalue ]

Undefines the lvalue. Always returns the undefined value.

Special Variables

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.

Special Arrays


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.

Helena Ahonen
Last modified: Wed Oct 20 09:22:29 EET DST 1999