Perl Reference Card Cheat Sheet: by Via
Perl Reference Card Cheat Sheet: by Via
e(E)
scientific notation
reverse(@a);
reverse @a
@a = sort{$ela <=>
sort numerically
berlin.de>.
http://www.physik.fu-berlin.de/~goerz/
g, G
shorter %e or %f /
$elb}(@a);
%E or %f
@a = split(/-/,$s);
$s = join(, @c);
join @a elements
signed octal
string of chars
u, x, X
@a2 = @a[1,2,6..9];
array slice
remove comments
1 Variable Types
into string
caps
1.1 Scalars and Strings
address pointer
chomp($str);
discard trailing \n
nothing printed
$v = chop($str);
$v becomes trailing
modifiers: h,l,L
char
string comparison
double
cmp
Perl image
More:
$str = 0 x 4;
q/STRING/,
$v = index($str, $x);
qq/STRING/, reverse,
$v = rindex($str, $x);
pack
uc, ucfirst
$v = substr($str,
extract substring
$strt, $len);
$cnt = $sky =~ tr/0-
9//;
$str =~ tr/a-zA-Z/
change non-alphas to
/cs;
space
$v = sprintf(%10s
format string
array initialization
$i = @a;
number of elements in
@a
swap $a and $b
$a);
$x = $a[1];
access to index 1
%[flags][0]
$i = $#a;
last index in @a
[width][.precision][mod]ty
push(@a, $s);
appends $s to @a
$a = pop(@a);
chop(@a);
$a = shift(@a);
%08d,$s,$n);
Format String:
from @a
pe
types:
c
character
d(i)
1.3 Hashes
%h=(k1 => val1,k2
hash initialization
=> 3);
$val = $map{k1};
recall value
@a = %h;
%h = @a;
foreach $k
(keys(%h)){..}
foreach $v
(vals(%h)){..}
values
while (($k,$v)=each
%h){..}
value-pairs
delete $h{k1};
delete key
exists $h{k1}
defined $h{k1}
is key defined?
Sponsored by CrosswordCheats.com
cheatography.com/mishin/
mishin.narod.ru
Page 1 of 6.
http://crosswordcheats.com
$aref = \@a;
reference to
closure, $c
exception
array
persists
if $@;
handling
$aref = [1,"foo",undef,13];
anonymous array
*foo{THING}
foo-syntax for
access element
@{$aref}[0];
of array
$aref2 = [@{$aref1}];
copy array
$href = \%h;
reference to
($var =~ /re/),
m/pattern/igmsox
anonymous hash
sheets/perlcheat/
qr/pattern/imsox
perl-reference-card
s/pattern/replace
access element
of hash
$href2 = {%{$href1}};
copy hash
if (ref($r) eq "HASH") {}
checks if $r
points to hash
2-dim array
http://www.cheatography.com/mishin/cheatsheets/perl-reference-card/
20-killer-perl-programming-tips
http://www.cheatography.com/mishin/cheat-
ment/igmsoxe
Modifiers:
i case-
o compile once
insensitive
sheets/20-killer-perl-programming-tips-
g global
x extended
for-beginners/
s as single line (.
e evaluate replacement
matches \n)
access 2-dim
array
2 Basic Syntax
hash of arrays
($a, $b) =
escape
shift(@ARGV);
params
define subroutine
start of line
end of line
,?
["h","m"]);
$name = $HoA{sp}[1];
matching pattern
http://www.cheatography.com/mishin/cheat-
%{$href}{APR};
%HoA=(fs=>["f","b"], sp=>
($var !~ /re/)
hash
$i = $a[0][1];
6 Regular Expressions
8};
creating refs
access to hash
of arrays
$fh = *STDIN
globref
$coderef = \&fnc;
Syntax:
shift; ...}
p(bla);
execute subroutine
if(expr){} elsif {}
conditional
else {}
anon subroutine
unless (expr){}
negative conditional
&$coderef();
calling anon
while (expr){}
while-loop
until (expr){}
until-loop
do {} until (expr)
postcheck until-loop
for($i=1; $i<=10;
for-loop
subroutine
$i++){}
nongreedy)
+, +?
?, ??
0 or 1 times (greedy /
nongreedy)
\b, \B
\A
foreach $i (@list){}
foreach-loop
\Z
\z
jump to top
By Nikolay Mishin (mishin)
Sponsored by CrosswordCheats.com
cheatography.com/mishin/
mishin.narod.ru
Page 2 of 6.
http://crosswordcheats.com
\G
\w, \W
(?<=..), (?
char
<!..)
[...]
character set
\s, \S
(?>...)
prohibit backtracking
(...)
\C
match a byte
(?{ code
embedded code
(?:...)
\pP, \PP
})
{n,m} , {n,m}?
p-named-unicode
(??{ code
dynamic regex
})
m times
\p{...},
{n,} , {n,}?
at least n times
\P{...}
named-unicode
(?
condition corresponding to
{n} , {n}?
exactly n times
\X
(cond)yes|
captured parentheses
or
Posix:
\1, \2
[:alnum:]
alphanumeric
...)
[:alpha:]
alphabetic
[:ascii:]
Escape Sequences:
\a alarm (beep)
\e escape
[:blank:]
whitespace [ \t]
\f formfeed
\n newline
[:cntrl:]
control characters
\r carriage return
\t tab
[:digit:]
digits
\cx control-x
[:graph:]
alphanum + punctuation
\L lowercase until \E
\U uppercase until \E
[:lower:]
lowercase chars
\Q diable metachars
\E end case
[:print:]
until \E
modifications
[:punct:]
punctuation
[:space:]
whitespace [\s\ck]
Character Classes:
[amy]
[:upper:]
uppercase chars
[f-j.-]
[:word:]
alphanum + '_'
[^f-j]
[:xdigit:]
hex digit
[:^digit:]
non-digit
f-j
\d, \D
no)
(?
(cond)yes)
around
Variables
$&
$`
$'
$1, $2 ...
$+
$^N
$^R
@-, @+
http://perldoc.perl.org/perlrequick.html
http://habrahabr.ru/post/17126/
Extended Constructs
(?#text)
comment
(?imxs-
imsx:...)
(?=...),
(?!...)
By Nikolay Mishin (mishin)
Sponsored by CrosswordCheats.com
cheatography.com/mishin/
mishin.narod.ru
Page 3 of 6.
http://crosswordcheats.com
5 Input/Output (cont)
use re 'taint';
File Tests:
open(PRC,"caesar <$file
read from
|");
process
open(EXTRACT, "|sort
write to process
-r, -w
readable, writeable
-x
executable
-e
exists
-f, -d, -l
is file, directory,
symlink
-T, -B
-M, -A
mod/access age in
days
system call
u>$f.s);
@a = readpipe(lsmod);
catch output
catch output
change root
while (<*.c>) {}
unlink(/tmp/file);
delete file
if (-f file.txt){...}
file test
@stats =
stat(filename);
status
http://www.devshed.co
$line = <INFILE>;
@lines = <INFILE>;
slurp infile
foreach $line
(<STDIN>){...}
STDIN
print to STDERR
1.\n";
close INFILE;
close filehandle
More:
m/c/a/Perl/File-Tests-
binmode, dbmopen,
select, syscall,
in-Perl/
sysreed, sysseek,
tell,
telldir,truncate,
seekdir
pack, unpack,
More:
chmod, chown,
opendir, readlink,
rename, rmdir,
vec
7 Object-Oriented Perl and Modules
5 Input/Output
open(INFILE,"in.txt") or
>Tmp$$");
die;
open(INFILE,"<:utf8","fil
my $Census;
e");
encoding
open(TMP, "+>",
open anonymous
my $class = shift;
undef);
temp file
open(MEMORY,'>',
open in-memory-file
\$var);
open(OUT,">out.txt") or
die;
open(LOG,">>my.log")
my $self = {};
$self->{NAME} = undef; # field
$self->{"_CENSUS"} = \$Census; # class data
++ ${ $self->{"_CENSUS"} };
bless ($self, $class);
return $self;
or die;
Sponsored by CrosswordCheats.com
cheatography.com/mishin/
mishin.narod.ru
Page 4 of 6.
http://crosswordcheats.com
my $self = shift;
8 One-Liners (cont)
8 One-Liners (cont)
-p
-l
return $self->{NAME};
}
sub DESTROY { #destructor
my $self = shift; -- ${$self->{"_CENSUS"} };}
Interactive
http://szabgab.com/using-the-
repl.html
-e
debugger
/perl-debugger/
The Perl
http://docstore.mik.ua/orelly/perl/pro
Debugger
g3/ch20_01.htm
-T
http://www.codeproject.com/Articles/3152/Perl-
Object-Oriented-Programming
http://ynonperek.com/course/perl/oo.html
commands).
http://perldoc.perl.org/perlrun.html
Perl flags -
$x;'
directory recursively.
https://twitter.com/#!/perloneliner
8 One-Liners
(zero) specify the input record separator
Examples:
a
-
is to be interpreted as a perl
Installing Modules:
each line.
-w
$him->name("Jason");
debug
built-in-debugger-of-perl-ashttp://www.thegeekstuff.com/2010/05
perl-
>name;
Mode:
contents of $_
-i
Sponsored by CrosswordCheats.com
cheatography.com/mishin/
mishin.narod.ru
Page 5 of 6.
http://crosswordcheats.com
Examples: (cont)
https://gist.github.com/563679
perl -MFile::Find -le 'find(sub{print
$File::Find::name if /\b[a-z]{2}_[A-Z]
{2}/},"/usr")'
Sponsored by CrosswordCheats.com
cheatography.com/mishin/
mishin.narod.ru
Page 6 of 6.
http://crosswordcheats.com