3 perlcheat - Perl 5 Cheat Sheet
7 This 'cheat sheet' is a handy reference, meant for beginning Perl
8 programmers. Not everything is mentioned, but 195 features may
9 already be overwhelming.
13 CONTEXTS SIGILS ref ARRAYS HASHES
14 void $scalar SCALAR @array %hash
15 scalar @array ARRAY @array[0, 2] @hash{'a', 'b'}
16 list %hash HASH $array[0] $hash{'a'}
18 *glob GLOB SCALAR VALUES
19 FORMAT number, string, ref, glob, undef
21 \ reference $$foo[1] aka $foo->[1]
22 $@%&* dereference $$foo{bar} aka $foo->{bar}
23 [] anon. arrayref ${$$foo[1]}[2] aka $foo->[1]->[2]
24 {} anon. hashref ${$$foo[1]}[2] aka $foo->[1][2]
27 OPERATOR PRECEDENCE foreach (LIST) { } for (a;b;c) { }
28 -> while (e) { } until (e) { }
29 ++ -- if (e) { } elsif (e) { } else { }
30 ** unless (e) { } elsif (e) { } else { }
31 ! ~ \ u+ u- given (e) { when (e) {} default {} }
33 * / % x NUMBERS vs STRINGS FALSE vs TRUE
34 + - . = = undef, "", 0, "0"
35 << >> + . anything else
36 named uops == != eq ne
37 < > <= >= lt gt le ge < > <= >= lt gt le ge
38 == != <=> eq ne cmp ~~ <=> cmp
40 | ^ REGEX MODIFIERS REGEX METACHARS
41 && /i case insensitive ^ string begin
42 || // /m line based ^$ $ str end (bfr \n)
43 .. ... /s . includes \n + one or more
44 ?: /x ignore wh.space * zero or more
45 = += last goto /p preserve ? zero or one
46 , => /a ASCII /aa safe {3,7} repeat in range
47 list ops /l locale /d dual | alternation
48 not /u Unicode [] character class
49 and /e evaluate /ee rpts \b word boundary
50 or xor /g global \z string end
51 /o compile pat once () capture
52 DEBUG (?:p) no capture
53 -MO=Deparse REGEX CHARCLASSES (?#t) comment
54 -MO=Terse . [^\n] (?=p) ZW pos ahead
55 -D## \s whitespace (?!p) ZW neg ahead
56 -d:Trace \w word chars (?<=p) ZW pos behind \K
57 \d digits (?<!p) ZW neg behind
58 CONFIGURATION \pP named property (?>p) no backtrack
59 perl -V:ivsize \h horiz.wh.space (?|p|p)branch reset
60 \R linebreak (?<n>p)named capture
61 \S \W \D \H negate \g{n} ref to named cap
64 stat localtime caller SPECIAL VARIABLES
65 0 dev 0 second 0 package $_ default variable
66 1 ino 1 minute 1 filename $0 program name
67 2 mode 2 hour 2 line $/ input separator
68 3 nlink 3 day 3 subroutine $\ output separator
69 4 uid 4 month-1 4 hasargs $| autoflush
70 5 gid 5 year-1900 5 wantarray $! sys/libcall error
71 6 rdev 6 weekday 6 evaltext $@ eval error
72 7 size 7 yearday 7 is_require $$ process ID
73 8 atime 8 is_dst 8 hints $. line number
74 9 mtime 9 bitmask @ARGV command line args
75 10 ctime 10 hinthash @INC include paths
76 11 blksz 3..10 only @_ subroutine args
77 12 blcks with EXPR %ENV environment
79 =head1 ACKNOWLEDGEMENTS
81 The first version of this document appeared on Perl Monks, where several
82 people had useful suggestions. Thank you, Perl Monks.
84 A special thanks to Damian Conway, who didn't only suggest important changes,
85 but also took the time to count the number of listed features and make a
86 Perl 6 version to show that Perl will stay Perl.
90 Juerd Waalboer <#####@juerd.nl>, with the help of many Perl Monks.
98 L<http://perlmonks.org/?node_id=216602> - the original PM post
102 L<http://perlmonks.org/?node_id=238031> - Damian Conway's Perl 6 version
106 L<http://juerd.nl/site.plp/perlcheat> - home of the Perl Cheat Sheet