Perl Cheat Sheet

This is a quick and dirty cheat sheet.

Global Scalar Variables

Default scalar $_
Program Name $0
Real user ID and Effective user ID $< and $>
Real group ID and Effective group ID $( and $)
Perl version number $]
Input line separator and output line separator $/ and $\
Output field separator $,
Array element separator $"
Number output format $#
Display the error message $@
Display the system error code $?
Display the system error message $!
Display the current line number $.
Multiline matching $*
First array subscript i.e first element is 0 $[
Word-Break specifier $:
Perl process ID $$
Program start time (number of seconds since 01/01/70 returned $^T

Pattern System Variables

Retrieving the Entire matched pattern $&
Retrieving the unmatched pattern $` (unmatched text preceding the match)
$'  (unmatched text following the match)
Matches the last subpattern enclosed in parentheses $+

File System Variables

Default Print Format $~
Specifying Page Length $=
Lines remaining on the page $-
Page header print format $^
Buffering output $|
Current page number $%

Array System Variables

List the arguements passed to a subroutine @_
List of arguements passed to the Perl program @ARGV
List of directories to be searched for files requested by the require function @INC
Associative array list the files requestyed by the require function that have already been found. %INC
Eviroment variables that have been defined for the Perl program %ENV
Send signals to other processes %SIG

Built in file Variables

Standard IN, OUT and Error STDIN, STDOUT, STDERR
Current input file ARGV
Used with the __END__ special value to read data at the end of a file DATA

Command-Line Options

Printing perl version perl -v
Printing warnings perl -w
Executing a single line program perl -e "print ('Hello)';
Excute a perl program perl -s
Supplying your own Command-Line Options to a perl program perl -s -potato="hot" (sets value potato to hot)
Operating on Multiple Files perl -n -e "print $_;" file1 file2 file3
Operating on files and printing perl -p -e ";" file1 file2 file3 (same as Operating on Multiple Files)
Editing single or multiple files perl -p -i -e "s/abc/def/g;" file1 file2 file3
Writing secure programs perl -T
using the perl Debugger perl -d

Escape Sequences (Character Strings)

Bell (beep) \a
Backspace \b
The ctrl+n character \cn
Escape \e
Ends the affect of \L, \U and \Q \E
Form Feed \f
Forces the next letter into lower case \l
All following letters are lower case \L
Newline \n
Carriage Return \r
Do not look for special pattern characters \Q
Tab \t
Force next letter into upper case \u
All following letters are upper case \U
Verital Tab \v

Perl Operators

Integer Operators
equal to ==
less than <
greater than >
less than equal to <=
greater than equal to >=
not equal to !=
comparison returning 1,0 or -1 <=>
String Operators
equal to eq
less than lt
greater than gt
less than equal to le
greater than equal to ge
not equal to ne
comparison returning 1,0 or -1 cmp
Logical Operators
OR ||
AND &&
Bitwise Operators
bitwise AND &
bitwise OR |
bitwise XOR ^
bitwise NOT ~
Left shift <<
Right shift >>
Assignment Operators
Associate or assignment =
addition and assignment +=
subtraction and assignment -=
multiplication and assignment *=
divison and assignment /=
remainder and assignment %=
exponentiation and assignment **=
bitwise AND and assignment &=
bitwise OR and assignment |=
bitwise XOR and assignment ^=
Autoincrement and Autodecrement Operators
increment ++
decrement --
String Operators
concatenation .
repetition x
Concatenation and assignment .=
Other Perl Operators
comma ,
conditional <value> == <value> ? <value> : <value>;

Operator Order of Precedence

Brackets ()
Autoincrement/Autodecrement ++, --
Operators with one operand - , ~, !
Exponentiation **
Pattern-Matching operators =~, !~
Multiplication, Division,Remainder and Repetition *, /, %, x
Addition, Subtraction, Concatenation +, -, .
Shifting operators <<, >>
File-Status operators -e, -r, etc
Inequality-comparison operators <, <=, >, >=, lt, le, gt, ge
Equality-comparison operators ==, !=, <=>, eq, ne, cmp
Bitwise AND &
Bitwise OR and XOR |, ^
Logical AND &&
Logical OR ||
List-Range operator ..
Conditional operator ? and :
Assignment operators =, +=, -=, *=, etc
Comma operator ,
low-precedence logical NOT not
low-precedence logical AND and
low-precedence logical OR and XOR or, xor

Pattern Matching

matches any character except the newline character .
means one or more of the preceding characters +
enable you to define patterns that match one of a group of alternatives []
match zero or more occurrences of the preceding character *
match zero or one occurrence of the preceding character ?
match at beginning of a string ^ or \A
match at the end of a string $ or \Z
match on word boundary \b
match inside a word \B
if you want to include a character that is normally treated as a special character \, \Q and \E
Excluding [^]
Any digit \d
Anything other than a digit \D
Any word character \w
Anything not a word character \W
White space \s
Anything other than a white space \S
Specified number of occurrences


Note: the format is {<minimum>,<maximum>}

specify choice |
Portition reuse


Note: first set stored in \1 (used in patern matching), or $1 (used when assign to variables)

Pattern Matching Precedence
Order of precedence ()             # Pattern memeory
+ * ? {}       # Number of occurrences
^ $ \b \B      # Pattern Anchors
|              # Alternatives
Pattern Matching Options
Match all possible patterns /g
Ignore case /i
Treat string as multiple lines /m
Only evaluate once /o
Treat string as single line /s
Ignore white space in pattern /x


Note: you can use any of the above pattern-matching options including the additional one below

/e - evaluate replacement string as expression



Note: you can use y/ instead of tr/, you have several options
/c  -  Translate all characters not specified
/d  -  Delete all specified characters
/s  -  Replace multiple identical ouput characters with a single

File-Test Operators

is name a block device -b
is name a character device -c
is name a directory -d
does name exists -e
is name a ordinary file -f
does name have setgid bit set -g
does name have its "sticky bit" set -k
is name a symbolic link -l
is name owned by the user -o
is name a named pipe -p
is name a readable file -r
is name a non-empty file -s
does name represent a terminal -t
does name have its setuid bit set -u
is name a writeable file -w
is name an executable file -x
is name a empty file -z
how long since name has been accessed -A
is name a binary file -B
how long since name inode has been accessed -C
how long since name has been modified -M
is the name owned by the "real user" only -O
is the name readable by the "real user" only -R
is the name a socket -S
is the name a text file -T
is the name writeable by the "real user" only -W
is the name executable by the "real user" only -X