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

AUTHORJuerd Waalboer, with the help of many Perl Monks.
the original PM posthttp:/­/pe­rlm­onk­s.o­rg/­?no­de_­id=­216602
Damian Conway's Perl 6 versionhttp:/­/pe­rlm­onk­s.o­rg/­?no­de_­id=­238031
home of the Perl Cheat Sheethttp:/­/ju­erd.nl­/si­te.p­lp­/pe­rlcheat

CONTEXTS

void
scalar
list

SIGILS

$scalar
@array
%hash
&sub
*glob

ARRAYS­,HASHES

 ARRAYSHASHES
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.orghttps:­//m­eta­cpa­n.org
Perl Weeklyhttp:/­/pe­rlw­eek­ly.c­om­/la­tes­t.html
Perl­Monks - The Monastery Gateshttp:/­/ww­w.p­erl­mon­ks.org/
What's happening in the world of Perl progra­mming, including Perl 5, Perl 6, the CPAN and Parrot?http:/­/pe­rlb­uzz.com/
The Compre­hensive Perl Archive Network - www.cp­an.orghttp:/­/cp­an.org
News and views of the Perl progra­mming languagehttp:/­/ww­w.p­erl.com/
Perl progra­mming docume­ntation - perldo­c.p­erl.orghttp:/­/pe­rld­oc.p­er­l.org/
Perl Confer­ences, YAPCs, Workshops, and Hackathonshttp:/­/ww­w.y­apc.org/
Perl 6http:/­/pe­rl6.org/
Perl 6 Tablet 2 - Basic Syntaxhttp:/­/ta­ble­ts.p­er­l6.o­rg­/ta­ble­t-2­-ba­sic­-sy­nta­x.html
Learn Perl - learn.p­er­l.orghttp:/­/le­arn.pe­rl.org/
Perl - Downloadhttp:/­/ww­w.p­erl.or­g/g­et.html
The Perl.org Online Libraryhttp:/­/ww­w.p­erl.or­g/b­ook­s/l­ibr­ary.html
Books and More (Videos)http:/­/ww­w.p­erl.or­g/m­edi­a.html
Padre, the Perl IDEhttp:/­/pa­dre.pe­rli­de.org/
perlcheathttp:/­/pe­rld­oc.p­er­l.o­rg/­per­lch­eat.html
Gabor Szabo Perl Trainer and develo­perhttp:/­/sz­abg­ab.com/
Perl,Perl Monger­s,M­ong­ers­,User Group,­Meetinghttp:/­/pm.org
Facebook vs Google+ for Perl projectshttp:/­/sz­abg­ab.c­om­/fa­ceb­ook­-vs­-go­ogl­e-p­lus­-fo­r-p­erl­-pr­oje­cts.html
Lections about Per from Gabor Szabohttp:/­/ww­w.y­out­ube.co­m/r­esu­lts­?se­arc­h_q­uer­y=g­abo­r+perl
mod_perl worldhttp:/­/pe­rl.a­pa­che.org
Interp­reted Languages: PHP, Perl, Python, Ruby (Sheet One)http:/­/hy­per­pol­ygl­ot.o­rg­/sc­ripting
Moose - A postmodern object system for Perlhttp:/­/mo­ose.ii­nte­rac­tiv­e.com/
Perl Training Australia - Perl Tipshttp:/­/pe­rlt­rai­nin­g.c­om.a­u/­tips/
Basic Perl (Ynon Perek | An Amazing Learning Experi­ence) , superhttp:/­/yn­onp­ere­k.c­om/­cou­rse­s/b­asi­c-p­erl.html
Perl Questions - Stack Overflowhttp:/­/st­ack­ove­rfl­ow.c­om­/qu­est­ion­s/t­agg­ed/­per­l?s­ort­=fa­q&­pag­esi­ze=15
Perl - Cheat sheets Collectionhttp:/­/pe­rl4­eve­ryt­hin­g.b­log­spo­t.c­om/­201­0/1­2/p­erl­-ch­eat­-sh­eet­s-c­oll­ect­ion.html
comp­lete, working examples of good Perl codehttp:/­/pe­rlm­eme.or­g/s­tar­t_h­ere­/in­dex.html
Perl Progra­mming Tutorialshttp:/­/ww­w.d­evs­hed.co­m/c­/b/­Perl/
Video from UniverTVhttp:/­/un­ive­rtv.ru­/se­arc­h/?­txt­=pe­rl&x=­0&y=0
FAQ по perl и програ­мми­рованию для webhttp:/­/ww­w.c­ybe­rgu­ru.r­u/­web­/we­b-p­rog­ram­min­g/p­erl­-we­b-p­rog­ram­min­g-f­aq-­pag­e2.html
FAQ PERL Agavahttp:/­/ho­sti­ng.a­ga­va.r­u/­faq­/te­ch/­doc­s/p­erl.shtml

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
\bword boundary
\zstring end
http:/­/pe­rld­oc.p­er­l.o­rg/­per­lre.html

Perl image

REGEX CHARCL­ASSES

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

REGEX MODIFIERS

/icase insens.
/mline based ^$
/s. includes \n
/xign. wh.space
/gglobal
\Qquote (disable) pattern metach­ara­cters till \E
\Eend either case modifi­cation or quoted section, think vi

FUNCTION RETURN LISTS

statlocaltimecaller
0 dev0 second0 package
1 ino1 minute1 filename
2 mode2 hour2 line
3 nlink3 day3 subroutine
4 uid4 month-14 hasargs
5 gid5 year-19005 wantarray
6 rdev6 weekday6 evaltext
7 size7 yearday7 is_require
8 atime8 is_dst8 hints
9 mtime 9 bitmask
10 ctimejust use
11 blkszPOSIX::3..9 only
12 blcksstrftime! http:/­/do­cst­ore.mi­k.u­a/o­rel­ly/­per­l/p­rog­3/c­h32­_36.htmwith 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

\ttab (HT, TAB)
\nnewline (LF, NL)
\rreturn (CR)
\fform feed (FF)
\aalarm (bell) (BEL)
\eescape (think troff) (ESC)
\cKcontrol char (example: VT)
\x{}, \x00character 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{}, \000character whose ordinal is the given octal number
\llowercase next char (think vi)
\uuppercase next char (think vi)
\Llowercase till \E (think vi)
\Uuppercase till \E (think vi)
\Qquote (disable) pattern metach­ara­cters till \E
\Eend 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
$0program name
$/input separator
$\output separator
$|autoflush
$!sys/li­bcall error
$@eval error
$$process ID
$.line number
@ARGVcommand line args
@INCinclude paths
@_subroutine args
%ENVenviro­nment

NUMBERS vs STRINGS

NUMBERSSTRINGS
==
..
== !=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.