Show Menu

perlcheat Cheat Sheet by mishin

This 'cheat sheet' is a handy reference, meant for beginning Perl programmers. Not everything is mentioned, but 194 features may already be overwhelming.http://search.cpan.org/~nwclark/perl-5.8.7/pod/perlcheat.pod

About Perlcheat

AUTHOR
Juerd Waalboer, with the help of many Perl Monks.
the original PM post
Damian Conway's Perl 6 version
home of the Perl Cheat Sheet

CONTEXTS

void
scalar
list

SIGILS

$scalar
@array
%hash
&sub
*glob

ARRAYS­,HASHES

 
ARRAYS
HASHES
whole:
@array
%hash
slice:
@array[0, 2]
@hash{'a', 'b'}
element:
$array[0]
$hash{'a'}

SCALAR VALUES

number, string, reference, glob, undef

SYNTAX

for (LIST) { }, for (a;b;c) { }
while ( ) { }, until ( ) { }
if ( ) { } elsif ( ) { } else { }
unless ( ) { } elsif ( ) { } else { }
for equals foreach (ALWAYS)

LINKS

Search the CPAN - metacp­an.org
Perl Weekly
Perl­Monks - The Monastery Gates
What's happening in the world of Perl progra­mming, including Perl 5, Perl 6, the CPAN and Parrot?
The Compre­hensive Perl Archive Network - www.cp­an.org
News and views of the Perl progra­mming language
Perl progra­mming docume­ntation - perldo­c.p­erl.org
Perl Confer­ences, YAPCs, Workshops, and Hackathons
Perl 6
Perl 6 Tablet 2 - Basic Syntax
Learn Perl - learn.p­er­l.org
Perl - Download
The Perl.org Online Library
Books and More (Videos)
Padre, the Perl IDE
perlcheat
Gabor Szabo Perl Trainer and develo­per
Perl,Perl Monger­s,M­ong­ers­,User Group,­Meeting
Facebook vs Google+ for Perl projects
Lections about Per from Gabor Szabo
mod_perl world
Interp­reted Languages: PHP, Perl, Python, Ruby (Sheet One)
Moose - A postmodern object system for Perl
Perl Training Australia - Perl Tips
Basic Perl (Ynon Perek | An Amazing Learning Experi­ence) , super
Perl Questions - Stack Overflow
Perl - Cheat sheets Collection
comp­lete, working examples of good Perl code
Perl Progra­mming Tutorials
Video from UniverTV
FAQ по perl и програ­мми­рованию для web
FAQ PERL Agava

Modern­::Perl

Modern­::Perl
enable all of the features of Modern Perl with one import
Toolkit
use Toolkit; and put all your favorite modules in a file: > cat $PERL5­LIB­/To­olk­it/­Mac­ros­/AL­WAY­S/M­odules use strict; use warnings; use Carp; use Smart:­:Co­mments;
use common­::s­ense;
supposed to be the same, with much lower memory usage, as: # # use strict qw(vars subs); # use feature qw(say state switch); # no warnings; # use warnings qw(FATAL closed threads internal debugging pack substr malloc # unopened portable prototype inplace io pipe unpack regexp # deprecated exiting glob digit printf utf8 layer # reserved parent­hesis taint closure semico­lon); # no warnings qw(exec newline);
latest
There is latest, which is no longer the latest. Demons­trates part of the naming problem.
uni::perl
Also, uni::perl which adds enabling unicode part of the mix.
ToolSet
ToolSet offers a subset of Toolkit's abilities, but without source filters.
Moose
I'll include Moose here, since it automa­tically adds strict and warnings to the calling package.
Acme::­Ver­y::­Mod­ern­::Perl
Using this module, you'll get Modern­::Perl, Toolkit, latest, uni::perl, perl5i, and common­::sense with just one simple line.
Source
 

REGEX METACHARS

^
string begin
$
str. end (before \n)
+
one or more
*
zero or more
?
zero or one
{3,7}
repeat in range
()
capture
(?:)
no capture
[]
character class
|
altern­ation
\b
word boundary
\z
string end

Perl image

REGEX CHARCL­ASSES

.
[^\n]
\s
[\x20\­f\t­\r\n]
\w
[A-Za-­z0-9_]
\d
[0-9]
\S, \W and \D
negate

REGEX MODIFIERS

/i
case insens.
/m
line based ^$
/s
. includes \n
/x
ign. wh.space
/g
global
\Q
quote (disable) pattern metach­ara­cters till \E
\E
end either case modifi­cation or quoted section, think vi

FUNCTION RETURN LISTS

stat
localtime
caller
0 dev
0 second
0 package
1 ino
1 minute
1 filename
2 mode
2 hour
2 line
3 nlink
3 day
3 subroutine
4 uid
4 month-1
4 hasargs
5 gid
5 year-1900
5 wantarray
6 rdev
6 weekday
6 evaltext
7 size
7 yearday
7 is_require
8 atime
8 is_dst
8 hints
9 mtime
 
9 bitmask
10 ctime
just use
11 blksz
POSIX::
3..9 only
12 blcks
with EXPR

opendir & grep:

opendi­r(D­IR,­"­$SC­AND­ATA­");
@files = sort(g­rep­(/[­0-9­]\.[0­-9]­\-­/,r­ead­dir­(DI­R)));
closed­ir(­DIR);

hash iteration:

while (($key­,$val) = each (%hash)) { }
 

Escape sequences

\t
tab (HT, TAB)
\n
newline (LF, NL)
\r
return (CR)
\f
form feed (FF)
\a
alarm (bell) (BEL)
\e
escape (think troff) (ESC)
\cK
control char (example: VT)
\x{}, \x00
character whose ordinal is the given hexade­cimal number
\N{name}
amed Unicode character or character sequence
\N{U+263D}
Unicode character (example: FIRST QUARTER MOON)
\o{}, \000
character whose ordinal is the given octal number
\l
lowercase next char (think vi)
\u
uppercase next char (think vi)
\L
lowercase till \E (think vi)
\U
uppercase till \E (think vi)
\Q
quote (disable) pattern metach­ara­cters till \E
\E
end either case modifi­cation or quoted section, think vi

REFERENCES

\
$$foo[1] ==
references
$foo->[1]
$@%&*
$$foo{­bar}==
derefe­rence
$foo->­{bar}
{}
${$$fo­o[1­]}[2]==
anon. hashref
$foo->­[1]­->[2]
\()
${$$fo­o[1­]}[2]==
list of refs
$foo->­[1][2]

SPECIAL VARIABLES

$_
default variable
$0
program name
$/
input separator
$\
output separator
$|
autoflush
$!
sys/li­bcall error
$@
eval error
$$
process ID
$.
line number
@ARGV
command line args
@INC
include paths
@_
subroutine args
%ENV
enviro­nment

NUMBERS vs STRINGS

NUMBERS
STRINGS
=
=
.
.
== !=
eq ne
< > <= >=
lt gt le ge
<=>
cmp

DO

use Modern­::Perl;
use strict;
use warnings;
my $var;
open() or die $!;
use Modules;

DON'T

"­$fo­o"
$$vari­abl­e_name
$user­input
/$user­input/

capita­lizing:

word: $foo = ucfirst lc $foo;
each word: $foo =~ s/(\w+­)/\­u\L­$1/g;

refere­ncing a subrou­tine:

$SUBNAME = "­jun­k";
&$­SUB­NAM­E(@­PAR­AME­TERS);

foreach:

foreach $FILE (@FILES) { }

OPERATOR PRECEDENCE

->
++ --
**
! ~ \ u+ u-
=~ !~
* / % x
+ - .
<< >>
named uops
< > <= >= lt gt le ge
== != <=> eq ne cmp
&
| ^
&&
||
.. ...
?:
= += -= *= etc.
, =>
list ops
not
and
or xor
 

Share This Cheat Sheet!

Favourited by 4 Members:

liyunzhen GarthZares sam chen gamiclea

Comments

DaveChild DaveChild, 08:34 2 Jun 12

Great cheat sheet, mishin! Loads of info :)

mishin mishin, 09:18 2 Jun 12

Thanks, Dave, cool engine!

Add a Comment

Your Comment

Please enter your name.

    Please enter your email address

      Please enter your Comment.