\input{../common/preamble} \begin{document} \perl % multicol parameters \raggedright \footnotesize \begin{multicols}{3} \setlength{\premulticols}{1pt} \setlength{\postmulticols}{1pt} \setlength{\multicolsep}{1pt} \setlength{\columnsep}{2pt} \begin{center} \textbf{\Large{Perl 5 Cheat Sheet}} \\ \end{center} ~~~ \texttt{a} is used for array's, \texttt{h} denotes a hash, \texttt{hr} a hash reference, \texttt{k/v} are key/value pairs and \texttt{e} is a element in a collection. \section{Conditions} \begin{compactitem} \item If \texttt{expr1} is \emph{true} then issue \texttt{stmt1}, else if \texttt{expr2} is \emph{true} issue \texttt{stmt2} else do \texttt{stmt3}: \begin{lstlisting}[frame=leftline] if (expr1) { stmt1; } elsif (expr2) { stmt2; } else { stmt3; } \end{lstlisting} \item Execute \texttt{stmt} if \texttt{\$var} is non-empty/non-zero string/number: \begin{lstlisting}[frame=leftline] if($var) { stmt; } \end{lstlisting} \item Execute \texttt{stmt} if \texttt{expr} is false: \begin{lstlisting}[frame=leftline] stmt unless(expr); # or unless(expr) { stmt; } \end{lstlisting} \item Switch statement (invokation of \texttt{last} ends futher matches): \begin{lstlisting}[frame=leftline] use Switch; switch ($val) { case 1 { print "number"; } case "a" { print "string"; } case (@a) { print "in-array"; } case (%h) { print "in-hash"; } case [1..6,9] { print "in-list"; last; } case /\w+/ { print "pattern"; } else { print "the-rest"; } } \end{lstlisting} \end{compactitem} \section{Operators} %TODO add this: @a = @b ? @b : @c; \subsection{Basic Operators} \begin{tabular}{@{}l>{\ttfamily}l} Arithmic operations: & + - * / \% \\ Exponent: & ** (fx: 2 ** 3 = 8) \\ Assignment: & = += -= *= /= \%= **= \\ Increment/Decrement & ++ -- \\ Bitwise and/or/xor: & \& | $\widehat{}$ \\ Bit complement, left/right shift: & $\sim$ << >> \end{tabular} \subsection{Comparison Operators} \begin{tabular}{@{}l>{\ttfamily}l} High precedence and/or/not: & \&\& || ! \\ Low precedence and/or/xor/not: & and or xor not \\ Arithmetic comparison: & == != > < >= <= \\ Pattern match/does not match: & $\sim$ !$\sim$ \\ String equality/inequality: & eq ne \\ True if exists/is file/is directory: & -x -f -d \end{tabular} \section{Arrays} \begin{tabular}{@{}l>{\ttfamily}l} Array containing two strings: & @a = ("s0","s1") \\ Array from comma-separated list: & @a = split(/,/, \$list) \\ First element of \texttt{@a}: & my \$e = \$a[0] \\ Replace third element: & \$a[2] = \$new \\ Add \texttt{\$e} to end of \texttt{@a}: & push(@a,\$e) \\ Delete last element from \texttt{@a}: & pop(@a) \\ Add \texttt{\$e} to front of \texttt{@a}: & unshift(@a,\$e) \\ Delete front element of \texttt{@a}: & shift(@array) \\ Loop through \texttt{@a} as \texttt{\$e}: & for my \$e (@a) \\ Loop through: \texttt{@a} as \texttt{\$\_}: & foreach (@a) \\ Sort in ascending order: & sort \{\$x<=>\$y\} (@a) \\ Sort in descending order: & sort \{\$y<=>\$x\} (@a) \\ Reverse array: & reverse(@array) \end{tabular} \section{Hashes} \begin{tabular}{@{}l>{\ttfamily}l} Empty hash: & my \%h = (); \\ Hash with two pairs: & \%h = ('k1'=>'v1','k2'=>'v2') \\ Add key/val to \texttt{\%h}: & \$h\{'k'\} = 'v' \\ Delete \texttt{\$k} from \texttt{\%h}: & delete \$h\{\$k\} \\ Loop \texttt{\%h} as \texttt{\$k/\$v}: & while (my (\$k,\$v) = each(\%h)) \\ Loop sorted \texttt{\%h} as \texttt{\$k}: & foreach my \$k (sort keys(\%h)) \end{tabular} \subsection{Hash References} \begin{tabular}{@{}l>{\ttfamily}l} Empty hash ref: & my \$hr; \\ Hash ref with two pairs: & \$hr = \{'k1'=>'v1','k2'=>'v2'\} \\ Add key/val to \texttt{\$hr}: & \$hr->\{'k'\} = 'v' \\ Delete \texttt{\$k} from \texttt{\$hr}: & delete \$hr->\{\$k\} \\ Loop \texttt{\$hr} as \texttt{\$k/\$v}: & while (my (\$k,\$v) = each(\%\$hr)) \\ Loop sorted ref \texttt{\$hr} as \texttt{\$k}: & foreach my \$k (sort keys(\%\$hr)) \end{tabular} \subsection{Hash Lookup} \begin{tabular}{@{}l>{\ttfamily}l} Value exists but may be undefined: & if exists(\$h->\{\$k\}) \\ Value is defined but may be false: & if defined(\$h->\{\$k\}) \\ Value is true: & if (\$h->\{\$k\}) \end{tabular} \section{Strings and Regular Expressions} \begin{tabular}{@{}l>{\ttfamily}l} Concatenate strings: & . \\ Concatenate and assign strings: & .= \\ Remove ending character: & chop(\$str); \\ Remove ending return character: & chomp(\$str); \\ Char position of \texttt{word}: & index(\$str,'word'); \\ Substring from 4th char to end: & substr(\$str, 4); \\ Substring from 3th last char to end: & substr(\$str, -3); \\ Substring from 4th char and 8 ahead: & substr(\$str, 8, 4); \end{tabular} % TODO clean up document when ready for release %\columnbreak \section{Functions} \begin{compactitem} \item Method accepting exactly one scalar, followed by an array: % TODO see if we can also force hashes this way, how about refs \begin{lstlisting}[frame=leftline] sub my_function($@) { my ($scalar,@array) = @_; : # do somthing useful } \end{lstlisting} \item Private method (only accessible inside module): \begin{lstlisting}[frame=leftline] my $private = sub { : # do something useful }; &$private(); # method invocation \end{lstlisting} \item Export method \texttt{my\_function}: \begin{lstlisting}[frame=leftline] use Exporter; our @ISA = qw(Exporter); our @EXPORT = qw( my_function # exported method ); \end{lstlisting} \end{compactitem} \section{Objects and Modules} \begin{compactitem} \item Perl object \emph{MyClass.pm} with constructor (\texttt{new}),\\ destructor (\texttt{DESTROY}) and a method (\texttt{my\_function}). \begin{lstlisting}[frame=leftline] package MyClass; sub new { my ($this, $arg1) = @_; my $self = { var => $arg1 }; bless $self, $this; return $self; } sub DESTROY { my ($this) = @_; : # free acquired resources } sub my_function { my ($this, $arg1) = @_; my $var = $this->{var}; : # do something useful } 1; \end{lstlisting} \item Use Perl object \texttt{MyClass}: \begin{lstlisting}[frame=leftline] use MyClass; my $obj = MyClass->new($var); # create $obj->my_function($arg); # invoke \end{lstlisting} \end{compactitem} \textinfo{1.1} \end{multicols} \end{document}