\documentclass[10pt,a4paper]{article} % Packages \usepackage{fancyhdr} % For header and footer \usepackage{multicol} % Allows multicols in tables \usepackage{tabularx} % Intelligent column widths \usepackage{tabulary} % Used in header and footer \usepackage{hhline} % Border under tables \usepackage{graphicx} % For images \usepackage{xcolor} % For hex colours %\usepackage[utf8x]{inputenc} % For unicode character support \usepackage[T1]{fontenc} % Without this we get weird character replacements \usepackage{colortbl} % For coloured tables \usepackage{setspace} % For line height \usepackage{lastpage} % Needed for total page number \usepackage{seqsplit} % Splits long words. %\usepackage{opensans} % Can't make this work so far. Shame. Would be lovely. \usepackage[normalem]{ulem} % For underlining links % Most of the following are not required for the majority % of cheat sheets but are needed for some symbol support. \usepackage{amsmath} % Symbols \usepackage{MnSymbol} % Symbols \usepackage{wasysym} % Symbols %\usepackage[english,german,french,spanish,italian]{babel} % Languages % Document Info \author{Nikolay Mishin (mishin)} \pdfinfo{ /Title (perl-reference-card.pdf) /Creator (Cheatography) /Author (Nikolay Mishin (mishin)) /Subject (Perl Reference Card Cheat Sheet) } % Lengths and widths \addtolength{\textwidth}{6cm} \addtolength{\textheight}{-1cm} \addtolength{\hoffset}{-3cm} \addtolength{\voffset}{-2cm} \setlength{\tabcolsep}{0.2cm} % Space between columns \setlength{\headsep}{-12pt} % Reduce space between header and content \setlength{\headheight}{85pt} % If less, LaTeX automatically increases it \renewcommand{\footrulewidth}{0pt} % Remove footer line \renewcommand{\headrulewidth}{0pt} % Remove header line \renewcommand{\seqinsert}{\ifmmode\allowbreak\else\-\fi} % Hyphens in seqsplit % This two commands together give roughly % the right line height in the tables \renewcommand{\arraystretch}{1.3} \onehalfspacing % Commands \newcommand{\SetRowColor}[1]{\noalign{\gdef\RowColorName{#1}}\rowcolor{\RowColorName}} % Shortcut for row colour \newcommand{\mymulticolumn}[3]{\multicolumn{#1}{>{\columncolor{\RowColorName}}#2}{#3}} % For coloured multi-cols \newcolumntype{x}[1]{>{\raggedright}p{#1}} % New column types for ragged-right paragraph columns \newcommand{\tn}{\tabularnewline} % Required as custom column type in use % Font and Colours \definecolor{HeadBackground}{HTML}{333333} \definecolor{FootBackground}{HTML}{666666} \definecolor{TextColor}{HTML}{333333} \definecolor{DarkBackground}{HTML}{7D86FF} \definecolor{LightBackground}{HTML}{EEEFFF} \renewcommand{\familydefault}{\sfdefault} \color{TextColor} % Header and Footer \pagestyle{fancy} \fancyhead{} % Set header to blank \fancyfoot{} % Set footer to blank \fancyhead[L]{ \noindent \begin{multicols}{3} \begin{tabulary}{5.8cm}{C} \SetRowColor{DarkBackground} \vspace{-7pt} {\parbox{\dimexpr\textwidth-2\fboxsep\relax}{\noindent \hspace*{-6pt}\includegraphics[width=5.8cm]{/web/www.cheatography.com/public/images/cheatography_logo.pdf}} } \end{tabulary} \columnbreak \begin{tabulary}{11cm}{L} \vspace{-2pt}\large{\bf{\textcolor{DarkBackground}{\textrm{Perl Reference Card Cheat Sheet}}}} \\ \normalsize{by \textcolor{DarkBackground}{Nikolay Mishin (mishin)} via \textcolor{DarkBackground}{\uline{cheatography.com/1008/cs/399/}}} \end{tabulary} \end{multicols}} \fancyfoot[L]{ \footnotesize \noindent \begin{multicols}{3} \begin{tabulary}{5.8cm}{LL} \SetRowColor{FootBackground} \mymulticolumn{2}{p{5.377cm}}{\bf\textcolor{white}{Cheatographer}} \\ \vspace{-2pt}Nikolay Mishin (mishin) \\ \uline{cheatography.com/mishin} \\ \uline{\seqsplit{mishin}.narod.ru} \end{tabulary} \vfill \columnbreak \begin{tabulary}{5.8cm}{L} \SetRowColor{FootBackground} \mymulticolumn{1}{p{5.377cm}}{\bf\textcolor{white}{Cheat Sheet}} \\ \vspace{-2pt}Published 2nd June, 2012.\\ Updated 13th May, 2016.\\ Page {\thepage} of \pageref{LastPage}. \end{tabulary} \vfill \columnbreak \begin{tabulary}{5.8cm}{L} \SetRowColor{FootBackground} \mymulticolumn{1}{p{5.377cm}}{\bf\textcolor{white}{Sponsor}} \\ \SetRowColor{white} \vspace{-5pt} %\includegraphics[width=48px,height=48px]{dave.jpeg} Measure your website readability!\\ www.readability-score.com \end{tabulary} \end{multicols}} \begin{document} \raggedright \raggedcolumns % Set font size to small. Switch to any value % from this page to resize cheat sheet text: % www.emerson.emory.edu/services/latex/latex_169.html \footnotesize % Small font. \begin{multicols*}{3} \begin{tabularx}{5.377cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{5.377cm}}{\bf\textcolor{white}{About}} \tn \SetRowColor{white} \mymulticolumn{1}{x{5.377cm}}{This is version 2 of the perl reference card. \newline % Row Count 1 (+ 1) (cl) 2008 Michael Goerz \textless{}goerz@physik.fu-berlin.de\textgreater{}. \newline % Row Count 3 (+ 2) \seqsplit{http://www.physik.fu-berlin}.de/\textasciitilde{}goerz/ \newline % Row Count 4 (+ 1) Information taken liberally from the perl documentation and various other sources. \newline % Row Count 6 (+ 2) You may freely distribute this document.% Row Count 7 (+ 1) } \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{p{0.4977 cm} p{0.4977 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{5.377cm}}{\bf\textcolor{white}{1 Variable Types}} \tn \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{x{2.4885 cm} x{2.4885 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{5.377cm}}{\bf\textcolor{white}{1.1 Scalars and Strings}} \tn % Row 0 \SetRowColor{LightBackground} chomp(\$str); & discard trailing \textbackslash{}n \tn % Row Count 1 (+ 1) % Row 1 \SetRowColor{white} \$v = chop(\$str); & \$v becomes trailing char \tn % Row Count 3 (+ 2) % Row 2 \SetRowColor{LightBackground} eq, ne, lt, gt, le, ge, cmp & string comparison \tn % Row Count 5 (+ 2) % Row 3 \SetRowColor{white} \$str = "0" x 4; & \$str is now "0000" \tn % Row Count 7 (+ 2) % Row 4 \SetRowColor{LightBackground} \$v = index(\$str, \$x); & find index of \$x in \$str, \tn % Row Count 9 (+ 2) % Row 5 \SetRowColor{white} \$v = rindex(\$str, \$x); & starting from left or right \tn % Row Count 11 (+ 2) % Row 6 \SetRowColor{LightBackground} \$v = substr(\$str, \$strt, \$len); & extract substring \tn % Row Count 13 (+ 2) % Row 7 \SetRowColor{white} \$cnt = \$sky =\textasciitilde{} tr/0-9//; & count the digits in \$sky \tn % Row Count 15 (+ 2) % Row 8 \SetRowColor{LightBackground} \$str =\textasciitilde{} tr/a-zA-Z/ /cs; & change non-alphas to space \tn % Row Count 17 (+ 2) % Row 9 \SetRowColor{white} \$v = sprintf("\%10s \%08d",\$s,\$n); & format string \tn % Row Count 19 (+ 2) % Row 10 \SetRowColor{LightBackground} Format String: & \%{[}flags{]}{[}0{]}{[}width{]}{[}.precision{]}{[}mod{]}type \tn % Row Count 21 (+ 2) % Row 11 \SetRowColor{white} \mymulticolumn{2}{x{5.377cm}}{types:} \tn % Row Count 22 (+ 1) % Row 12 \SetRowColor{LightBackground} c & character \tn % Row Count 23 (+ 1) % Row 13 \SetRowColor{white} d(i) & signed decimal int \tn % Row Count 24 (+ 1) % Row 14 \SetRowColor{LightBackground} e(E) & scientific notation \tn % Row Count 25 (+ 1) % Row 15 \SetRowColor{white} f & decimal floating point \tn % Row Count 27 (+ 2) % Row 16 \SetRowColor{LightBackground} g, G & shorter \%e or \%f / \%E or \%f \tn % Row Count 29 (+ 2) % Row 17 \SetRowColor{white} o & signed octal \tn % Row Count 30 (+ 1) \end{tabularx} \par\addvspace{1.3em} \vfill \columnbreak \begin{tabularx}{5.377cm}{x{2.4885 cm} x{2.4885 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{5.377cm}}{\bf\textcolor{white}{1.1 Scalars and Strings (cont)}} \tn % Row 18 \SetRowColor{LightBackground} s & string of chars \tn % Row Count 1 (+ 1) % Row 19 \SetRowColor{white} u, x, X & unsigned decimal int / hex int / hex int in caps \tn % Row Count 4 (+ 3) % Row 20 \SetRowColor{LightBackground} p & address pointer \tn % Row Count 5 (+ 1) % Row 21 \SetRowColor{white} n & nothing printed \tn % Row Count 6 (+ 1) % Row 22 \SetRowColor{LightBackground} modifiers: h,l,L & arg is short int / long int, double/ long double \tn % Row Count 9 (+ 3) % Row 23 \SetRowColor{white} \mymulticolumn{2}{x{5.377cm}}{More:} \tn % Row Count 10 (+ 1) % Row 24 \SetRowColor{LightBackground} chr, crypt, hex, lc, lcfirst, length, oct, ord, pack & q/STRING/, qq/STRING/, reverse, uc, ucfirst \tn % Row Count 13 (+ 3) \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{x{2.53827 cm} x{2.43873 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{5.377cm}}{\bf\textcolor{white}{1.2 Arrays and Lists}} \tn % Row 0 \SetRowColor{LightBackground} @a = (1..5); & array initialization \tn % Row Count 2 (+ 2) % Row 1 \SetRowColor{white} \$i = @a; & number of elements in @a \tn % Row Count 4 (+ 2) % Row 2 \SetRowColor{LightBackground} (\$a, \$b) = (\$b, \$a); & swap \$a and \$b \tn % Row Count 5 (+ 1) % Row 3 \SetRowColor{white} \$x = \$a{[}1{]}; & access to index 1 \tn % Row Count 6 (+ 1) % Row 4 \SetRowColor{LightBackground} \$i = \$\#a; & last index in @a \tn % Row Count 7 (+ 1) % Row 5 \SetRowColor{white} push(@a, \$s); & appends \$s to @a \tn % Row Count 8 (+ 1) % Row 6 \SetRowColor{LightBackground} \$a = pop(@a); & removes last element \tn % Row Count 10 (+ 2) % Row 7 \SetRowColor{white} chop(@a); & remove last char (per el.) \tn % Row Count 12 (+ 2) % Row 8 \SetRowColor{LightBackground} \$a = shift(@a); & removes first element \tn % Row Count 14 (+ 2) % Row 9 \SetRowColor{white} reverse(@a); & reverse @a \tn % Row Count 15 (+ 1) % Row 10 \SetRowColor{LightBackground} @a = sort\{\$ela \textless{}=\textgreater{} \$elb\}(@a); & sort numerically \tn % Row Count 17 (+ 2) % Row 11 \SetRowColor{white} @a = split(/-/,\$s); & split string into @a \tn % Row Count 19 (+ 2) % Row 12 \SetRowColor{LightBackground} \$s = join(", " @c); & join @a elements into string \tn % Row Count 21 (+ 2) % Row 13 \SetRowColor{white} @a2 = @a{[}1,2,6..9{]}; & array slice \tn % Row Count 22 (+ 1) % Row 14 \SetRowColor{LightBackground} @a2 = grep(!/\textasciicircum{}\#/, @a); & remove comments from @a \tn % Row Count 24 (+ 2) \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{5.377cm}}{\bf\textcolor{white}{Perl image}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{p{5.377cm}}{\vspace{1px}\centerline{\includegraphics[width=5.1cm]{/web/www.cheatography.com/public/uploads/mishin_1338568208_index.jpg}}} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{x{2.58804 cm} x{2.38896 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{5.377cm}}{\bf\textcolor{white}{1.3 Hashes}} \tn % Row 0 \SetRowColor{LightBackground} \%h=(k1 =\textgreater{} "val1",k2 =\textgreater{} 3); & hash initialization \tn % Row Count 2 (+ 2) % Row 1 \SetRowColor{white} \$val = \$map\{k1\}; & recall value \tn % Row Count 3 (+ 1) % Row 2 \SetRowColor{LightBackground} @a = \%h; & array of keys and values \tn % Row Count 5 (+ 2) % Row 3 \SetRowColor{white} \%h = @a; & create hash from array \tn % Row Count 7 (+ 2) % Row 4 \SetRowColor{LightBackground} foreach \$k (keys(\%h))\{..\} & iterate over list of keys \tn % Row Count 9 (+ 2) % Row 5 \SetRowColor{white} foreach \$v (vals(\%h))\{..\} & iterate over list of values \tn % Row Count 11 (+ 2) % Row 6 \SetRowColor{LightBackground} while ((\$k,\$v)=each \%h)\{..\} & iterate over key-value-pairs \tn % Row Count 13 (+ 2) % Row 7 \SetRowColor{white} delete \$h\{k1\}; & delete key \tn % Row Count 14 (+ 1) % Row 8 \SetRowColor{LightBackground} exists \$h\{k1\} & does key exist? \tn % Row Count 15 (+ 1) % Row 9 \SetRowColor{white} defined \$h\{k1\} & is key defined? \tn % Row Count 16 (+ 1) \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \columnbreak \begin{tabularx}{5.377cm}{x{2.93643 cm} x{2.04057 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{5.377cm}}{\bf\textcolor{white}{3 References and Data Structures}} \tn % Row 0 \SetRowColor{LightBackground} \$aref = \textbackslash{}@a; & reference to array \tn % Row Count 2 (+ 2) % Row 1 \SetRowColor{white} \$aref = {[}1,"foo",undef,13{]}; & anonymous array \tn % Row Count 4 (+ 2) % Row 2 \SetRowColor{LightBackground} \$el = \$aref-\textgreater{}{[}0{]}; \$el = @\{\$aref\}{[}0{]}; & access element of array \tn % Row Count 6 (+ 2) % Row 3 \SetRowColor{white} \$aref2 = {[}@\{\$aref1\}{]}; & copy array \tn % Row Count 7 (+ 1) % Row 4 \SetRowColor{LightBackground} \$href = \textbackslash{}\%h; & reference to hash \tn % Row Count 9 (+ 2) % Row 5 \SetRowColor{white} \$href =\{APR =\textgreater{} 4,AUG =\textgreater{} 8\}; & anonymous hash \tn % Row Count 11 (+ 2) % Row 6 \SetRowColor{LightBackground} \$el = \$href-\textgreater{}\{APR\}; \$el = \%\{\$href\}\{APR\}; & access element of hash \tn % Row Count 13 (+ 2) % Row 7 \SetRowColor{white} \$href2 = \{\%\{\$href1\}\}; & copy hash \tn % Row Count 14 (+ 1) % Row 8 \SetRowColor{LightBackground} if (ref(\$r) eq "HASH") \{\} & checks if \$r points to hash \tn % Row Count 16 (+ 2) % Row 9 \SetRowColor{white} @a = ({[}1, 2{]},{[}3, 4{]}); & 2-dim array \tn % Row Count 17 (+ 1) % Row 10 \SetRowColor{LightBackground} \$i = \$a{[}0{]}{[}1{]}; & access 2-dim array \tn % Row Count 19 (+ 2) % Row 11 \SetRowColor{white} \%HoA=(fs=\textgreater{}{[}"f","b"{]}, sp=\textgreater{}{[}"h","m"{]}); & hash of arrays \tn % Row Count 21 (+ 2) % Row 12 \SetRowColor{LightBackground} \$name = \$HoA\{sp\}{[}1{]}; & access to hash of arrays \tn % Row Count 23 (+ 2) % Row 13 \SetRowColor{white} \$fh = *STDIN & globref \tn % Row Count 24 (+ 1) % Row 14 \SetRowColor{LightBackground} \$coderef = \textbackslash{}\&fnc; & code ref (e.g. callback) \tn % Row Count 26 (+ 2) % Row 15 \SetRowColor{white} \$coderef =sub\{print "bla"\}; & anon subroutine \tn % Row Count 28 (+ 2) % Row 16 \SetRowColor{LightBackground} \&\$coderef(); & calling anon subroutine \tn % Row Count 30 (+ 2) \end{tabularx} \par\addvspace{1.3em} \vfill \columnbreak \begin{tabularx}{5.377cm}{x{2.93643 cm} x{2.04057 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{5.377cm}}{\bf\textcolor{white}{3 References and Data Structures (cont)}} \tn % Row 17 \SetRowColor{LightBackground} sub createcnt\{ my \$c=shift; return sub \{ print "\$c++"; \}; \} & closure, \$c persists \tn % Row Count 4 (+ 4) % Row 18 \SetRowColor{white} *foo\{THING\} & foo-syntax for creating refs \tn % Row Count 6 (+ 2) \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{5.377cm}}{\bf\textcolor{white}{Link to perl cheat}} \tn % Row 0 \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{perlcheat} \tn \mymulticolumn{1}{x{5.377cm}}{\hspace*{6 px}\rule{2px}{6px}\hspace*{6 px}\seqsplit{http://www.cheatography.com/mishin/cheat-sheets/perlcheat/}} \tn % Row Count 3 (+ 3) % Row 1 \SetRowColor{white} \mymulticolumn{1}{x{5.377cm}}{perl-reference-card} \tn \mymulticolumn{1}{x{5.377cm}}{\hspace*{6 px}\rule{2px}{6px}\hspace*{6 px}\seqsplit{http://www.cheatography.com/mishin/cheat-sheets/perl-reference-card/}} \tn % Row Count 6 (+ 3) % Row 2 \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{\seqsplit{20-killer-perl-programming-tips}} \tn \mymulticolumn{1}{x{5.377cm}}{\hspace*{6 px}\rule{2px}{6px}\hspace*{6 px}\seqsplit{http://www.cheatography.com/mishin/cheat-sheets/20-killer-perl-programming-tips-for-beginners/}} \tn % Row Count 9 (+ 3) \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{x{2.4885 cm} x{2.4885 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{5.377cm}}{\bf\textcolor{white}{2 Basic Syntax}} \tn % Row 0 \SetRowColor{LightBackground} (\$a, \$b) = shift(@ARGV); & read command line params \tn % Row Count 2 (+ 2) % Row 1 \SetRowColor{white} sub p\{my \$var = shift; ...\} & define subroutine \tn % Row Count 4 (+ 2) % Row 2 \SetRowColor{LightBackground} p("bla"); & execute subroutine \tn % Row Count 5 (+ 1) % Row 3 \SetRowColor{white} if(expr)\{\} elsif \{\} else \{\} & conditional \tn % Row Count 7 (+ 2) % Row 4 \SetRowColor{LightBackground} unless (expr)\{\} & negative conditional \tn % Row Count 8 (+ 1) % Row 5 \SetRowColor{white} while (expr)\{\} & while-loop \tn % Row Count 9 (+ 1) % Row 6 \SetRowColor{LightBackground} until (expr)\{\} & until-loop \tn % Row Count 10 (+ 1) % Row 7 \SetRowColor{white} do \{\} until (expr) & postcheck until-loop \tn % Row Count 11 (+ 1) % Row 8 \SetRowColor{LightBackground} for(\$i=1; \$i\textless{}=10; \$i++)\{\} & for-loop \tn % Row Count 13 (+ 2) % Row 9 \SetRowColor{white} foreach \$i (@list)\{\} & foreach-loop \tn % Row Count 14 (+ 1) % Row 10 \SetRowColor{LightBackground} last, next, redo & end loop, skip to next, jump to top \tn % Row Count 16 (+ 2) % Row 11 \SetRowColor{white} eval \{\$a=\$a/\$b; \}; warn \$@ if \$@; & exception handling \tn % Row Count 18 (+ 2) \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \columnbreak \begin{tabularx}{5.377cm}{x{2.18988 cm} x{2.78712 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{5.377cm}}{\bf\textcolor{white}{6 Regular Expressions}} \tn % Row 0 \SetRowColor{LightBackground} (\$var =\textasciitilde{} /re/), (\$var !\textasciitilde{} /re/) & matches / does not match \tn % Row Count 2 (+ 2) % Row 1 \SetRowColor{white} \seqsplit{m/pattern/igmsoxc} & matching pattern \tn % Row Count 3 (+ 1) % Row 2 \SetRowColor{LightBackground} qr/pattern/imsox & store regex in variable \tn % Row Count 5 (+ 2) % Row 3 \SetRowColor{white} \seqsplit{s/pattern/replacement/igmsoxe} & search and replace \tn % Row Count 7 (+ 2) % Row 4 \SetRowColor{LightBackground} \mymulticolumn{2}{x{5.377cm}}{{\bf{Modifiers:}}} \tn % Row Count 8 (+ 1) % Row 5 \SetRowColor{white} i case-insensitive & o compile once \tn % Row Count 10 (+ 2) % Row 6 \SetRowColor{LightBackground} g global & x extended \tn % Row Count 11 (+ 1) % Row 7 \SetRowColor{white} s as single line (. matches \textbackslash{}n) & e evaluate replacement \tn % Row Count 13 (+ 2) % Row 8 \SetRowColor{LightBackground} \mymulticolumn{2}{x{5.377cm}}{{\bf{Syntax:}}} \tn % Row Count 14 (+ 1) % Row 9 \SetRowColor{white} \textbackslash{} & escape \tn % Row Count 15 (+ 1) % Row 10 \SetRowColor{LightBackground} . & any single char \tn % Row Count 16 (+ 1) % Row 11 \SetRowColor{white} \textasciicircum{} & start of line \tn % Row Count 17 (+ 1) % Row 12 \SetRowColor{LightBackground} \$ & end of line \tn % Row Count 18 (+ 1) % Row 13 \SetRowColor{white} {\emph{, }}? & 0 or more times (greedy / nongreedy) \tn % Row Count 20 (+ 2) % Row 14 \SetRowColor{LightBackground} +, +? & 1 or more times (greedy / nongreedy) \tn % Row Count 22 (+ 2) % Row 15 \SetRowColor{white} ?, ?? & 0 or 1 times (greedy / nongreedy) \tn % Row Count 24 (+ 2) % Row 16 \SetRowColor{LightBackground} \textbackslash{}b, \textbackslash{}B & word boundary ( \textbackslash{}w - \textbackslash{}W) / match except at w.b. \tn % Row Count 27 (+ 3) % Row 17 \SetRowColor{white} \textbackslash{}A & string start (with /m) \tn % Row Count 28 (+ 1) % Row 18 \SetRowColor{LightBackground} \textbackslash{}Z & string end (before \textbackslash{}n) \tn % Row Count 29 (+ 1) % Row 19 \SetRowColor{white} \textbackslash{}z & absolute string end \tn % Row Count 30 (+ 1) \end{tabularx} \par\addvspace{1.3em} \vfill \columnbreak \begin{tabularx}{5.377cm}{x{2.18988 cm} x{2.78712 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{5.377cm}}{\bf\textcolor{white}{6 Regular Expressions (cont)}} \tn % Row 20 \SetRowColor{LightBackground} \textbackslash{}G & continue from previous m//g \tn % Row Count 2 (+ 2) % Row 21 \SetRowColor{white} {[}...{]} & character set \tn % Row Count 3 (+ 1) % Row 22 \SetRowColor{LightBackground} (...) & group, capture to \$1, \$2 \tn % Row Count 5 (+ 2) % Row 23 \SetRowColor{white} (?:...) & group without capturing \tn % Row Count 7 (+ 2) % Row 24 \SetRowColor{LightBackground} \{n,m\} , \{n,m\}? & at least n times, at most m times \tn % Row Count 9 (+ 2) % Row 25 \SetRowColor{white} \{n,\} , \{n,\}? & at least n times \tn % Row Count 10 (+ 1) % Row 26 \SetRowColor{LightBackground} \{n\} , \{n\}? & exactly n times \tn % Row Count 11 (+ 1) % Row 27 \SetRowColor{white} | & or \tn % Row Count 12 (+ 1) % Row 28 \SetRowColor{LightBackground} \textbackslash{}1, \textbackslash{}2 & text from nth group (\$1, ...) \tn % Row Count 14 (+ 2) % Row 29 \SetRowColor{white} \mymulticolumn{2}{x{5.377cm}}{{\bf{Escape Sequences:}}} \tn % Row Count 15 (+ 1) % Row 30 \SetRowColor{LightBackground} \textbackslash{}a alarm (beep) & \textbackslash{}e escape \tn % Row Count 16 (+ 1) % Row 31 \SetRowColor{white} \textbackslash{}f formfeed & \textbackslash{}n newline \tn % Row Count 17 (+ 1) % Row 32 \SetRowColor{LightBackground} \textbackslash{}r carriage return & \textbackslash{}t tab \tn % Row Count 19 (+ 2) % Row 33 \SetRowColor{white} \textbackslash{}cx control-x & \textbackslash{}l lowercase next char \tn % Row Count 20 (+ 1) % Row 34 \SetRowColor{LightBackground} \textbackslash{}L lowercase until \textbackslash{}E & \textbackslash{}U uppercase until \textbackslash{}E \tn % Row Count 22 (+ 2) % Row 35 \SetRowColor{white} \textbackslash{}Q diable metachars until \textbackslash{}E & \textbackslash{}E end case modifications \tn % Row Count 24 (+ 2) % Row 36 \SetRowColor{LightBackground} \mymulticolumn{2}{x{5.377cm}}{{\bf{Character Classes:}}} \tn % Row Count 25 (+ 1) % Row 37 \SetRowColor{white} {[}amy{]} & 'a', 'm', or 'y' \tn % Row Count 26 (+ 1) % Row 38 \SetRowColor{LightBackground} {[}f-j.-{]} & range f-j, dot, and dash \tn % Row Count 28 (+ 2) % Row 39 \SetRowColor{white} {[}\textasciicircum{}f-j{]} & everything except range f-j \tn % Row Count 30 (+ 2) \end{tabularx} \par\addvspace{1.3em} \vfill \columnbreak \begin{tabularx}{5.377cm}{x{2.18988 cm} x{2.78712 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{5.377cm}}{\bf\textcolor{white}{6 Regular Expressions (cont)}} \tn % Row 40 \SetRowColor{LightBackground} \textbackslash{}d, \textbackslash{}D & digit {[}0-9{]} / non-digit \tn % Row Count 2 (+ 2) % Row 41 \SetRowColor{white} \textbackslash{}w, \textbackslash{}W & word char {[}a-zA-Z0-9\_{]} / non-word char \tn % Row Count 4 (+ 2) % Row 42 \SetRowColor{LightBackground} \textbackslash{}s, \textbackslash{}S & whitepace {[} \textbackslash{}t\textbackslash{}n\textbackslash{}r\textbackslash{}f{]} / non-space \tn % Row Count 6 (+ 2) % Row 43 \SetRowColor{white} \textbackslash{}C & match a byte \tn % Row Count 7 (+ 1) % Row 44 \SetRowColor{LightBackground} \textbackslash{}pP, \textbackslash{}PP & match p-named unicode / non-p-named-unicode \tn % Row Count 9 (+ 2) % Row 45 \SetRowColor{white} \textbackslash{}p\{...\}, \textbackslash{}P\{...\} & match long-named unicode / non-named-unicode \tn % Row Count 11 (+ 2) % Row 46 \SetRowColor{LightBackground} \textbackslash{}X & match extended unicode \tn % Row Count 12 (+ 1) % Row 47 \SetRowColor{white} \mymulticolumn{2}{x{5.377cm}}{{\bf{Posix:}}} \tn % Row Count 13 (+ 1) % Row 48 \SetRowColor{LightBackground} {[}:alnum:{]} & alphanumeric \tn % Row Count 14 (+ 1) % Row 49 \SetRowColor{white} {[}:alpha:{]} & alphabetic \tn % Row Count 15 (+ 1) % Row 50 \SetRowColor{LightBackground} {[}:ascii:{]} & any ASCII char \tn % Row Count 16 (+ 1) % Row 51 \SetRowColor{white} {[}:blank:{]} & whitespace {[} \textbackslash{}t{]} \tn % Row Count 17 (+ 1) % Row 52 \SetRowColor{LightBackground} {[}:cntrl:{]} & control characters \tn % Row Count 18 (+ 1) % Row 53 \SetRowColor{white} {[}:digit:{]} & digits \tn % Row Count 19 (+ 1) % Row 54 \SetRowColor{LightBackground} {[}:graph:{]} & alphanum + punctuation \tn % Row Count 20 (+ 1) % Row 55 \SetRowColor{white} {[}:lower:{]} & lowercase chars \tn % Row Count 21 (+ 1) % Row 56 \SetRowColor{LightBackground} {[}:print:{]} & alphanum, punct, space \tn % Row Count 22 (+ 1) % Row 57 \SetRowColor{white} {[}:punct:{]} & punctuation \tn % Row Count 23 (+ 1) % Row 58 \SetRowColor{LightBackground} {[}:space:{]} & whitespace {[}\textbackslash{}s\textbackslash{}ck{]} \tn % Row Count 24 (+ 1) % Row 59 \SetRowColor{white} {[}:upper:{]} & uppercase chars \tn % Row Count 25 (+ 1) % Row 60 \SetRowColor{LightBackground} {[}:word:{]} & alphanum + '\_' \tn % Row Count 26 (+ 1) % Row 61 \SetRowColor{white} {[}:xdigit:{]} & hex digit \tn % Row Count 27 (+ 1) % Row 62 \SetRowColor{LightBackground} {[}:\textasciicircum{}digit:{]} & non-digit \tn % Row Count 28 (+ 1) % Row 63 \SetRowColor{white} \mymulticolumn{2}{x{5.377cm}}{{\bf{Extended Constructs}}} \tn % Row Count 29 (+ 1) % Row 64 \SetRowColor{LightBackground} (?\#text) & comment \tn % Row Count 30 (+ 1) \end{tabularx} \par\addvspace{1.3em} \vfill \columnbreak \begin{tabularx}{5.377cm}{x{2.18988 cm} x{2.78712 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{5.377cm}}{\bf\textcolor{white}{6 Regular Expressions (cont)}} \tn % Row 65 \SetRowColor{LightBackground} (?imxs-imsx:...) & enable or disable option \tn % Row Count 2 (+ 2) % Row 66 \SetRowColor{white} (?=...), (?!...) & positive / negative look-ahead \tn % Row Count 4 (+ 2) % Row 67 \SetRowColor{LightBackground} (?\textless{}=..), (?\textless{}!..) & positive / negative look-behind \tn % Row Count 6 (+ 2) % Row 68 \SetRowColor{white} (?\textgreater{}...) & prohibit backtracking \tn % Row Count 7 (+ 1) % Row 69 \SetRowColor{LightBackground} (?\{ code \}) & embedded code \tn % Row Count 8 (+ 1) % Row 70 \SetRowColor{white} (??\{ code \}) & dynamic regex \tn % Row Count 9 (+ 1) % Row 71 \SetRowColor{LightBackground} (?(cond)yes|no) & condition corresponding to captured parentheses \tn % Row Count 12 (+ 3) % Row 72 \SetRowColor{white} (?(cond)yes) & condition corresponding to look-around \tn % Row Count 14 (+ 2) % Row 73 \SetRowColor{LightBackground} \mymulticolumn{2}{x{5.377cm}}{{\bf{Variables}}} \tn % Row Count 15 (+ 1) % Row 74 \SetRowColor{white} \$\& & entire matched string \tn % Row Count 16 (+ 1) % Row 75 \SetRowColor{LightBackground} \$` & everything prior to matched string \tn % Row Count 18 (+ 2) % Row 76 \SetRowColor{white} \$' & everything after matched string \tn % Row Count 20 (+ 2) % Row 77 \SetRowColor{LightBackground} \$1, \$2 ... & n-th captured expression \tn % Row Count 22 (+ 2) % Row 78 \SetRowColor{white} \$+ & last parenthesis pattern match \tn % Row Count 24 (+ 2) % Row 79 \SetRowColor{LightBackground} \$\textasciicircum{}N & most recently closed capt. \tn % Row Count 26 (+ 2) % Row 80 \SetRowColor{white} \$\textasciicircum{}R & result of last (?\{...\}) \tn % Row Count 28 (+ 2) % Row 81 \SetRowColor{LightBackground} @-, @+ & offsets of starts / ends of groups \tn % Row Count 30 (+ 2) \end{tabularx} \par\addvspace{1.3em} \vfill \columnbreak \begin{tabularx}{5.377cm}{x{2.18988 cm} x{2.78712 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{5.377cm}}{\bf\textcolor{white}{6 Regular Expressions (cont)}} \tn % Row 82 \SetRowColor{LightBackground} \mymulticolumn{2}{x{5.377cm}}{\seqsplit{http://perldoc.perl.org/perlrequick.html}} \tn % Row Count 1 (+ 1) % Row 83 \SetRowColor{white} \mymulticolumn{2}{x{5.377cm}}{\seqsplit{http://habrahabr.ru/post/17126/}} \tn % Row Count 2 (+ 1) \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{5.377cm}}{\bf\textcolor{white}{Debugging regexp}} \tn \SetRowColor{white} \mymulticolumn{1}{x{5.377cm}}{use re 'taint'; \newline % Row Count 1 (+ 1) \# Contents of \$match are tainted if \$dirty was also tainted. \newline % Row Count 3 (+ 2) (\$match) = (\$dirty =\textasciitilde{} /\textasciicircum{}(.*)\$/s); \newline % Row Count 4 (+ 1) \# Allow code interpolation: \newline % Row Count 5 (+ 1) use re 'eval'; \newline % Row Count 6 (+ 1) \$pat = '(?\{ \$var = 1 \})'; \# embedded code execution \newline % Row Count 8 (+ 2) /alpha\$\{pat\}omega/; \# won't fail unless under -T \newline % Row Count 10 (+ 2) \# and \$pat is tainted \newline % Row Count 12 (+ 2) use re 'debug'; \# like "perl -Dr" \newline % Row Count 13 (+ 1) /\textasciicircum{}(.*)\$/s; \# output debugging info during \newline % Row Count 15 (+ 2) \# compile time and run time \newline % Row Count 17 (+ 2) use re 'debugcolor'; \# same as 'debug', \newline % Row Count 19 (+ 2) \# but with colored output% Row Count 21 (+ 2) } \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{x{2.4885 cm} x{2.4885 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{5.377cm}}{\bf\textcolor{white}{4 System Interaction}} \tn % Row 0 \SetRowColor{LightBackground} system("cat \$f|sort -u\textgreater{}\$f.s"); & system call \tn % Row Count 2 (+ 2) % Row 1 \SetRowColor{white} @a = readpipe("lsmod"); & catch output \tn % Row Count 4 (+ 2) % Row 2 \SetRowColor{LightBackground} \$today = "Today: ".`date`; & catch output \tn % Row Count 6 (+ 2) % Row 3 \SetRowColor{white} \mymulticolumn{2}{x{5.377cm}}{better: use IPC::Open3 'open3';!} \tn % Row Count 7 (+ 1) % Row 4 \SetRowColor{LightBackground} \seqsplit{chroot("/home/user/");} & change root \tn % Row Count 9 (+ 2) % Row 5 \SetRowColor{white} while (\textless{}*.c\textgreater{}) \{\} & operate on all c-files \tn % Row Count 11 (+ 2) % Row 6 \SetRowColor{LightBackground} \seqsplit{unlink("/tmp/file");} & delete file \tn % Row Count 13 (+ 2) % Row 7 \SetRowColor{white} if (-f "file.txt")\{...\} & file test \tn % Row Count 15 (+ 2) % Row 8 \SetRowColor{LightBackground} \mymulticolumn{2}{x{5.377cm}}{{\bf{File Tests:}}} \tn % Row Count 16 (+ 1) % Row 9 \SetRowColor{white} -r, -w & readable, writeable \tn % Row Count 17 (+ 1) % Row 10 \SetRowColor{LightBackground} -x & executable \tn % Row Count 18 (+ 1) % Row 11 \SetRowColor{white} -e & exists \tn % Row Count 19 (+ 1) % Row 12 \SetRowColor{LightBackground} -f, -d, -l & is file, directory, symlink \tn % Row Count 21 (+ 2) % Row 13 \SetRowColor{white} -T, -B & text file, binary file \tn % Row Count 23 (+ 2) % Row 14 \SetRowColor{LightBackground} -M, -A & mod/access age in days \tn % Row Count 25 (+ 2) % Row 15 \SetRowColor{white} @stats = stat("filename"); & 13-element list with status \tn % Row Count 27 (+ 2) % Row 16 \SetRowColor{LightBackground} {\bf{File Tests in Perl}} & \seqsplit{http://www.devshed.com/c/a/Perl/File-Tests-in-Perl/} \tn % Row Count 30 (+ 3) \end{tabularx} \par\addvspace{1.3em} \vfill \columnbreak \begin{tabularx}{5.377cm}{x{2.4885 cm} x{2.4885 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{5.377cm}}{\bf\textcolor{white}{4 System Interaction (cont)}} \tn % Row 17 \SetRowColor{LightBackground} \mymulticolumn{2}{x{5.377cm}}{More:} \tn % Row Count 1 (+ 1) % Row 18 \SetRowColor{white} chmod, chown, chroot, fcntl, glob, ioctl, link, lstat, mkdir, & opendir, readlink, rename, rmdir, symlink, umask, utime \tn % Row Count 5 (+ 4) \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{x{2.4885 cm} x{2.4885 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{5.377cm}}{\bf\textcolor{white}{5 Input/Output}} \tn % Row 0 \SetRowColor{LightBackground} open(INFILE,"in.txt") or die; & open file for input \tn % Row Count 2 (+ 2) % Row 1 \SetRowColor{white} open(INFILE,"\textless{}:utf8","file"); & open file with encoding \tn % Row Count 4 (+ 2) % Row 2 \SetRowColor{LightBackground} open(TMP, "+\textgreater{}", undef); & open anonymous temp file \tn % Row Count 6 (+ 2) % Row 3 \SetRowColor{white} open(MEMORY,'\textgreater{}', \textbackslash{}\$var); & open in-memory-file \tn % Row Count 8 (+ 2) % Row 4 \SetRowColor{LightBackground} open(OUT,"\textgreater{}out.txt") or die; & open output file \tn % Row Count 10 (+ 2) % Row 5 \SetRowColor{white} open(LOG,"\textgreater{}\textgreater{}my.log") or die; & open file for append \tn % Row Count 12 (+ 2) % Row 6 \SetRowColor{LightBackground} open(PRC,"caesar \textless{}\$file |"); & read from process \tn % Row Count 14 (+ 2) % Row 7 \SetRowColor{white} open(EXTRACT, "|sort \textgreater{}Tmp\$\$"); & write to process \tn % Row Count 16 (+ 2) % Row 8 \SetRowColor{LightBackground} \$line = \textless{}INFILE\textgreater{}; & get next line \tn % Row Count 17 (+ 1) % Row 9 \SetRowColor{white} @lines = \textless{}INFILE\textgreater{}; & slurp infile \tn % Row Count 18 (+ 1) % Row 10 \SetRowColor{LightBackground} foreach \$line (\textless{}STDIN\textgreater{})\{...\} & loop of lines from STDIN \tn % Row Count 20 (+ 2) % Row 11 \SetRowColor{white} print STDERR "Warning 1.\textbackslash{}n"; & print to STDERR \tn % Row Count 22 (+ 2) % Row 12 \SetRowColor{LightBackground} close INFILE; & close filehandle \tn % Row Count 23 (+ 1) % Row 13 \SetRowColor{white} \mymulticolumn{2}{x{5.377cm}}{More:} \tn % Row Count 24 (+ 1) % Row 14 \SetRowColor{LightBackground} binmode, dbmopen, dbmclose, fileno, flock, format, getc, read, readdir, readline, rewinddir, seek, seekdir & select, syscall, sysreed, sysseek, tell, telldir,truncate, pack, unpack, vec \tn % Row Count 30 (+ 6) \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \columnbreak \begin{tabularx}{5.377cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{5.377cm}}{\bf\textcolor{white}{7 Object-Oriented Perl and Modules}} \tn \SetRowColor{white} \mymulticolumn{1}{x{5.377cm}}{{\bf{Defining a new class:}} \newline % Row Count 1 (+ 1) package Person; \newline % Row Count 2 (+ 1) use strict; \newline % Row Count 3 (+ 1) my \$Census; \newline % Row Count 4 (+ 1) sub new \{ \#constructor, any name is fine \newline % Row Count 5 (+ 1) my \$class = shift; \newline % Row Count 6 (+ 1) my \$self = \{\}; \newline % Row Count 7 (+ 1) \$self-\textgreater{}\{NAME\} = undef; \# field \newline % Row Count 8 (+ 1) \$self-\textgreater{}\{"\_CENSUS"\} = \textbackslash{}\$Census; \# class data \newline % Row Count 10 (+ 2) ++ \$\{ \$self-\textgreater{}\{"\_CENSUS"\} \}; \newline % Row Count 11 (+ 1) bless (\$self, \$class); \newline % Row Count 12 (+ 1) return \$self; \newline % Row Count 13 (+ 1) \} \newline % Row Count 14 (+ 1) sub name \{ \#method \newline % Row Count 15 (+ 1) my \$self = shift; \newline % Row Count 16 (+ 1) if (@\_) \{ \$self-\textgreater{}\{NAME\} = shift \} \newline % Row Count 17 (+ 1) return \$self-\textgreater{}\{NAME\}; \newline % Row Count 18 (+ 1) \} \newline % Row Count 19 (+ 1) sub DESTROY \{ \#destructor \newline % Row Count 20 (+ 1) my \$self = shift; -{}- \$\{\$self-\textgreater{}\{"\_CENSUS"\} \};\} \newline % Row Count 22 (+ 2) 1; \# so the 'require' or 'use' succeeds \newline % Row Count 24 (+ 2) {\bf{Using the class:}} \newline % Row Count 25 (+ 1) use Person; \newline % Row Count 26 (+ 1) \$him = Person-\textgreater{}new(); \newline % Row Count 27 (+ 1) \$him-\textgreater{}name("Jason"); \newline % Row Count 28 (+ 1) printf "There's someone named \%s.\textbackslash{}n", \$him-\textgreater{}name; \newline % Row Count 29 (+ 1) use Data::Dumper; print Dumper(\$him); \# debug \newline % Row Count 30 (+ 1) } \tn \end{tabularx} \par\addvspace{1.3em} \vfill \columnbreak \begin{tabularx}{5.377cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{5.377cm}}{\bf\textcolor{white}{7 Object-Oriented Perl and Modules (cont)}} \tn \SetRowColor{white} \mymulticolumn{1}{x{5.377cm}}{\seqsplit{http://www.codeproject.com/Articles/3152/Perl-Object-Oriented-Programming} \newline % Row Count 2 (+ 2) \seqsplit{http://ynonperek.com/course/perl/oo.html}% Row Count 3 (+ 1) } \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{5.377cm}}{\bf\textcolor{white}{Installing Modules:}} \tn \SetRowColor{white} \mymulticolumn{1}{x{5.377cm}}{perl -MCPAN -e shell;% Row Count 1 (+ 1) } \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{x{2.4885 cm} x{2.4885 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{5.377cm}}{\bf\textcolor{white}{8 One-Liners}} \tn % Row 0 \SetRowColor{LightBackground} -0 & (zero) specify the input record separator \tn % Row Count 3 (+ 3) % Row 1 \SetRowColor{white} -a & split data into an array named @F \tn % Row Count 5 (+ 2) % Row 2 \SetRowColor{LightBackground} -F & specify pattern for -a to use when splitting \tn % Row Count 8 (+ 3) % Row 3 \SetRowColor{white} -i & edit files in place \tn % Row Count 9 (+ 1) % Row 4 \SetRowColor{LightBackground} -n & run through all the @ARGV arguments as files, using \textless{}\textgreater{} \tn % Row Count 12 (+ 3) % Row 5 \SetRowColor{white} -p & same as -n, but will also print the contents of \$\_ \tn % Row Count 15 (+ 3) % Row 6 \SetRowColor{LightBackground} Interactive Mode: & perl -de1;use Term::ReadKey; \tn % Row Count 17 (+ 2) % Row 7 \SetRowColor{white} & \seqsplit{http://szabgab.com/using-the-built-in-debugger-of-perl-as-repl.html} \tn % Row Count 21 (+ 4) % Row 8 \SetRowColor{LightBackground} perl-debugger & \seqsplit{http://www.thegeekstuff.com/2010/05/perl-debugger/} \tn % Row Count 24 (+ 3) % Row 9 \SetRowColor{white} The Perl Debugger & \seqsplit{http://docstore.mik.ua/orelly/perl/prog3/ch20\_01.htm} \tn % Row Count 27 (+ 3) % Row 10 \SetRowColor{LightBackground} -T & enables taint checking, which instructs perl to keep track of data from the user and avoid doing anything insecure with it. Here this option is used to avoid taking the current directory name from the @INC variable and listing the available .pm files from the directory recursively. \tn % Row Count 42 (+ 15) \end{tabularx} \par\addvspace{1.3em} \vfill \columnbreak \begin{tabularx}{5.377cm}{x{2.4885 cm} x{2.4885 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{5.377cm}}{\bf\textcolor{white}{8 One-Liners (cont)}} \tn % Row 11 \SetRowColor{LightBackground} -l & enables automatic line-ending processing in the output. Print statements will have the new line separator (\textbackslash{}n) added at the end of each line. \tn % Row Count 8 (+ 8) % Row 12 \SetRowColor{white} -w & prints any warning messages. \tn % Row Count 10 (+ 2) % Row 13 \SetRowColor{LightBackground} -e & indicates that the following string is to be interpreted as a perl script (i.e., sequence of commands). \tn % Row Count 16 (+ 6) % Row 14 \SetRowColor{white} \mymulticolumn{2}{x{5.377cm}}{\seqsplit{http://perldoc.perl.org/perlrun.html}} \tn % Row Count 17 (+ 1) % Row 15 \SetRowColor{LightBackground} Perl flags -pe, -pi, -p, -w, -d, -i, -t? perldoc perlrun & perl -e '\$x = "Hello world!n"; print \$x;' \tn % Row Count 20 (+ 3) % Row 16 \SetRowColor{white} & perl -MO=Deparse -p -e 1 \tn % Row Count 22 (+ 2) % Row 17 \SetRowColor{LightBackground} & perl -MO=Deparse -p -i -e 1 \tn % Row Count 24 (+ 2) % Row 18 \SetRowColor{white} & perl -MO=Deparse -p -i.bak -e 1 \tn % Row Count 26 (+ 2) % Row 19 \SetRowColor{LightBackground} \mymulticolumn{2}{x{5.377cm}}{https://twitter.com/\#!/perloneliner} \tn % Row Count 27 (+ 1) \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{5.377cm}}{\bf\textcolor{white}{Examples:}} \tn % Row 0 \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{1. just lines 15 to 17, efficiently} \tn \mymulticolumn{1}{x{5.377cm}}{\hspace*{6 px}\rule{2px}{6px}\hspace*{6 px}perl -ne 'print if \$. \textgreater{}= 15; exit if \$. \textgreater{}= 17;'} \tn % Row Count 2 (+ 2) % Row 1 \SetRowColor{white} \mymulticolumn{1}{x{5.377cm}}{2. just lines NOT between line 10 and 20} \tn \mymulticolumn{1}{x{5.377cm}}{\hspace*{6 px}\rule{2px}{6px}\hspace*{6 px}perl -ne 'print unless 10 .. 20'} \tn % Row Count 4 (+ 2) % Row 2 \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{3. lines between START and END} \tn \mymulticolumn{1}{x{5.377cm}}{\hspace*{6 px}\rule{2px}{6px}\hspace*{6 px}perl -ne 'print if /\textasciicircum{}START\$/ .. /\textasciicircum{}END\$/'} \tn % Row Count 6 (+ 2) % Row 3 \SetRowColor{white} \mymulticolumn{1}{x{5.377cm}}{4. in-place edit of *.c files changing all foo to bar} \tn \mymulticolumn{1}{x{5.377cm}}{\hspace*{6 px}\rule{2px}{6px}\hspace*{6 px}perl -pi.bak -e 's/\textbackslash{}bfoo\textbackslash{}b/bar/g' *.c} \tn % Row Count 9 (+ 3) % Row 4 \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{5. delete first 10 lines} \tn \mymulticolumn{1}{x{5.377cm}}{\hspace*{6 px}\rule{2px}{6px}\hspace*{6 px}perl -i.old -ne 'print unless 1 .. 10' foo.txt} \tn % Row Count 11 (+ 2) % Row 5 \SetRowColor{white} \mymulticolumn{1}{x{5.377cm}}{6. change all the isolated oldvar occurrences to newvar} \tn \mymulticolumn{1}{x{5.377cm}}{\hspace*{6 px}\rule{2px}{6px}\hspace*{6 px}perl -i.old -pe 's\{\textbackslash{}boldvar\textbackslash{}b\}\{newvar\}g' *.{[}chy{]}} \tn % Row Count 15 (+ 4) % Row 6 \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{7. printing each line in reverse order} \tn \mymulticolumn{1}{x{5.377cm}}{\hspace*{6 px}\rule{2px}{6px}\hspace*{6 px}perl -e 'print reverse \textless{}\textgreater{}' file1 file2 file3 ....} \tn % Row Count 18 (+ 3) % Row 7 \SetRowColor{white} \mymulticolumn{1}{x{5.377cm}}{8. find palindromes in the /usr/dict/words dictionary file} \tn \mymulticolumn{1}{x{5.377cm}}{\hspace*{6 px}\rule{2px}{6px}\hspace*{6 px}perl -lne '\$\_ = lc \$\_; print if \$\_ eq reverse' /usr/dict/words} \tn % Row Count 22 (+ 4) % Row 8 \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{9. command-line that reverses all the bytes in a file} \tn \mymulticolumn{1}{x{5.377cm}}{\hspace*{6 px}\rule{2px}{6px}\hspace*{6 px}perl -0777e 'print scalar reverse \textless{}\textgreater{}' f1 f2 f3} \tn % Row Count 25 (+ 3) % Row 9 \SetRowColor{white} \mymulticolumn{1}{x{5.377cm}}{10. word wrap between 50 and 72 chars} \tn \mymulticolumn{1}{x{5.377cm}}{\hspace*{6 px}\rule{2px}{6px}\hspace*{6 px}perl -p000e 'tr/ \textbackslash{}t\textbackslash{}n\textbackslash{}r/ /; s/(.\{50,72\})\textbackslash{}s/\$1\textbackslash{}n/g;\$\_.="\textbackslash{}n"x2'} \tn % Row Count 28 (+ 3) % Row 10 \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{11. strip and remove double spaces} \tn \mymulticolumn{1}{x{5.377cm}}{\hspace*{6 px}\rule{2px}{6px}\hspace*{6 px}perl -pe '\$\_ = " \$\_ "; tr/ \textbackslash{}t/ /s; \$\_ = substr(\$\_,1,-1)'} \tn % Row Count 31 (+ 3) \end{tabularx} \par\addvspace{1.3em} \vfill \columnbreak \begin{tabularx}{5.377cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{5.377cm}}{\bf\textcolor{white}{Examples: (cont)}} \tn % Row 11 \SetRowColor{white} \mymulticolumn{1}{x{5.377cm}}{12. move '{\emph{.txt.out' to '}}.out'} \tn \mymulticolumn{1}{x{5.377cm}}{\hspace*{6 px}\rule{2px}{6px}\hspace*{6 px}perl -e '(\$n = \$\_) =\textasciitilde{} s/\textbackslash{}.txt(\textbackslash{}.out)\$/\$1/ and not -e \$n and rename \$\_, \$n for @ARGV' *} \tn % Row Count 4 (+ 4) % Row 12 \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{13. write a hash slice, which we have come as a reference to a hash} \tn \mymulticolumn{1}{x{5.377cm}}{\hspace*{6 px}\rule{2px}{6px}\hspace*{6 px}perl -E'my \$h=\{1..8\}; say for @\{\$h\}\{1,3,5,7\}'} \tn % Row Count 7 (+ 3) % Row 13 \SetRowColor{white} \mymulticolumn{1}{x{5.377cm}}{14. If you had installed any modules from CPAN, then you will need to re-install all of them. (Naveed Massjouni)} \tn \mymulticolumn{1}{x{5.377cm}}{\hspace*{6 px}\rule{2px}{6px}\hspace*{6 px}perl -E 'say for grep /site\_perl/,@INC'| xargs find | perl -Fsite\_perl/ -lane 'print \$F{[}1{]} if /\textbackslash{}.pm\$/' | cpanm -{}-reinstall} \tn % Row Count 13 (+ 6) % Row 14 \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{15. Give executable rights to all perl file in dir} \tn \mymulticolumn{1}{x{5.377cm}}{\hspace*{6 px}\rule{2px}{6px}\hspace*{6 px}find \seqsplit{/home/client0/public\_html} -type f -name '*.pl' -print0 | xargs -0 chmod 0755} \tn % Row Count 16 (+ 3) % Row 15 \SetRowColor{white} \mymulticolumn{1}{x{5.377cm}}{16. Find files matching name-pattern \seqsplit{https://gist.github.com/563679}} \tn \mymulticolumn{1}{x{5.377cm}}{\hspace*{6 px}\rule{2px}{6px}\hspace*{6 px}perl -MFile::Find -le 'find(sub\{print \$File::Find::name if /\textbackslash{}b{[}a-z{]}\{2\}\_{[}A-Z{]}\{2\}/\},"/usr")'} \tn % Row Count 20 (+ 4) \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} % That's all folks \end{multicols*} \end{document}