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]
26 NUMBERS vs STRINGS LINKS
27 OPERATOR PRECEDENCE = = perldoc.perl.org
28 -> + . search.cpan.org
29 ++ -- == != eq ne cpan.org
30 ** < > <= >= lt gt le ge pm.org
31 ! ~ \ u+ u- <=> cmp p3rl.org
34 + - . foreach (LIST) { } for (a;b;c) { }
35 << >> while (e) { } until (e) { }
36 named uops if (e) { } elsif (e) { } else { }
37 < > <= >= lt gt le ge unless (e) { } elsif (e) { } else { }
38 == != <=> eq ne cmp ~~ given (e) { when (e) {} default {} }
40 | ^ REGEX METACHARS REGEX MODIFIERS
41 && ^ string begin /i case insensitive
42 || // $ str end (bfr \n) /m line based ^$
43 .. ... + one or more /s . includes \n
44 ?: * zero or more /x ignore wh.space
45 = += -= *= etc ? zero or one /p preserve
46 , => {3,7} repeat in range /a ASCII /aa safe
47 list ops | alternation /l locale /d dual
48 not [] character class /u Unicode
49 and \b word boundary /e evaluate /ee rpts
50 or xor \z string end /g global
51 () capture /o compile pat once
52 DEBUG (?:p) no capture
53 -MO=Deparse (?#t) comment REGEX CHARCLASSES
54 -MO=Terse (?=p) ZW pos ahead . [^\n]
55 -D## (?!p) ZW neg ahead \s whitespace
56 -d:Trace (?<=p) ZW pos behind \K \w word chars
57 (?<!p) ZW neg behind \d digits
58 CONFIGURATION (?>p) no backtrack \pP named property
59 perl -V:ivsize (?|p|p)branch reset \h horiz.wh.space
60 (?&NM) cap to name \R linebreak
63 stat localtime caller SPECIAL VARIABLES
64 0 dev 0 second 0 package $_ default variable
65 1 ino 1 minute 1 filename $0 program name
66 2 mode 2 hour 2 line $/ input separator
67 3 nlink 3 day 3 subroutine $\ output separator
68 4 uid 4 month-1 4 hasargs $| autoflush
69 5 gid 5 year-1900 5 wantarray $! sys/libcall error
70 6 rdev 6 weekday 6 evaltext $@ eval error
71 7 size 7 yearday 7 is_require $$ process ID
72 8 atime 8 is_dst 8 hints $. line number
73 9 mtime 9 bitmask @ARGV command line args
74 10 ctime 10 hinthash @INC include paths
75 11 blksz 3..10 only @_ subroutine args
76 12 blcks with EXPR %ENV environment
78 =head1 ACKNOWLEDGEMENTS
80 The first version of this document appeared on Perl Monks, where several
81 people had useful suggestions. Thank you, Perl Monks.
83 A special thanks to Damian Conway, who didn't only suggest important changes,
84 but also took the time to count the number of listed features and make a
85 Perl 6 version to show that Perl will stay Perl.
89 Juerd Waalboer <#####@juerd.nl>, with the help of many Perl Monks.
97 L<http://perlmonks.org/?node_id=216602> - the original PM post
101 L<http://perlmonks.org/?node_id=238031> - Damian Conway's Perl 6 version
105 L<http://juerd.nl/site.plp/perlcheat> - home of the Perl Cheat Sheet