Perl 5 Cheat Sheet
by Arjen Laarhoven, based on Juerd Waalboer's cheat sheet
Contexts
Sigils
Arrays
Hashes
Scalar values
References
Operator precedence
Numbers/Strings
Regex metacharacters
Regex modifiers
Regex character classes
Function return lists
Special variables
Do
Don't
Links
CONTEXTS
void scalar list
SIGILS
$scalar @array %hash
&sub *glob
ARRAYS
@array @array[0, 2]
$array[0]
HASHES
%hash $hash{'a'}
@hash{'a','b'}
SCALAR VALUES
number string reference
glob undef
REFERENCES
\ reference
$@%&* dereference
[] anon arrayref
{} anon hashref
\() list of refs
$$foo[1] => $foo->[1]
$$foo{bar} => $foo->{bar}
${$$foo[1]}[2]
=> $foo->[1]->[2]
${$$foo[1]}[2]
=> $foo->[1][2]
Operator Precedence
->
++ --
**
! ~ / u+ u-
=~ !~
* / % x
+ - .
<< >>
named uops
< > <= >= lt gt le ge
== != <=> eq ne cmp
&
| ^
&&
||
.. ...
?:
= += -= *= etc.
list ops
not
and
or
xor
NUMBERS/STRINGS
NUM STR
= =
+ .
== != eq ne
< > <= >= lt gt le ge
<=> cmp
REGEX METACHARACTERS
^ string begin
$ string end
+ one or more
* zero or more
? zero or one
{3,7} repeat in range
() capture
(?:) no capture
[] character class
| alternation
\b word boundary
\z string end
REGEX MODIFIERS
/i case insens.
/m line based ^$
/s . includes \n
/x ignore whitespace
/g global
REGEX CHARCLASSES
. == [^\n]
\s == [\x20\f\t\r\n]
\w == [A-Za-z0-9_]
\d == [0-9]
\S, \W and \D negate
FUNCTION RETURN LISTS
stat localtime
0 dev 0 second
1 ino 1 minute
2 mode 2 hour
3 nlink 3 day
4 uid 4 mon-1
5 gid 5 yr-1900
6 rdev 6 wday
7 size 7 yrday
8 atime 8 is_dst
9 mtime
10 ctime
11 blksz
12 blcks
caller
0 package 5 wantarray
1 filename 6 evaltext
2 line 7 is_require
3 subroutine 8 hints
4 hashargs 9 bitmask
0..9 only with EXPR
SPECIAL VARIABLES
$_ default variable
$0 program name
$/ input separator
$\ output separator
$| autoflush
$! sys/libcall error
$@ eval error
$$ process ID
$. line number
@ARGV command line args
@INC include paths
@_ subroutine args
%ENV environment
DO
use strict;
use warnings;
my $var;
open() or die $!;
use Modules;
DON'T
"$foo"
$$variable_name
`$userinput`
/$userinput/
LINKS
perl.plover.com
search.cpan.org
cpan.org
pm.org
tpj.com
perldoc.com
perl.com
perlmonks.org
use.perl.org
perl.apache.org
parrotcode.org