Perl Quick Reference Card version 0.01 – editor: John Bokma – freelance programmer DRAFT VERSION, check: http://johnbokma.com/perl/
Backslashed Character Escapes \n Newline (usually LF) \e \r Carriage return (usually CR) \033 \t Horizontal tab \x7f \f Form feed \cC \b Backspace \x{263a} \a Alert (bell) \N{NAME}
61 ESC character ESC in octal DEL in hexadecimal Control-C Unicode (smiley) Named character
Operator Precedence (continued) Associativiy Arity Precedence Class Right 3 ?: Right 2 = += -= *= and so on Left 2 , => Right 0+ List operators (rightward) Right 1 not Left 2 and Left 2 or xor
87
File Test Operators 98 -r File is readable by effective UID/GID. Translation Escapes 61 -w File is writable by effective UID/GID. \u Force next character to uppercase (“titlecase” in Unicode). -x File is executable by effective UID/GID. \l Force next character to lowercase. -o File is owned by effective UID/GID. \U Force all following characters to uppercase -R File is readable by real UID/GID. \L Force all following characters to lowercase -W File is writable by real UID/GID. \Q Backslash all following nonalphanumeric characters. -X File is executable by real UID/GID. \E End \U, \L, or \Q. -O File is owned by real UID/GID. Quote Constructs 63 -e File exists. -z File has zero size Customary Generic Meaning Interpolates -s File has nonzero size (returns size). -f File is a plain file. '' q// Literal string No -d File is a directory. "" qq// Literal string Yes -l File is a symbolic link. `` qx// Command execution Yes -p File is a named pipe (FIFO). () qw// Word list No -S File is a socket. // m// Pattern match Yes -b File is a block special file. s/// s/// Pattern substitution Yes -c File is a character special file. y/// tr/// Character translation No -t Filehandle is open to a tty. "" qr// Regular expression Yes -u File has setuid bit set. -g File has setgid bit set. Note: no interpolation is done if you use single quotes for delimiters. -k File has sticky bit set. Operator Precedence 87 -T File is a text file. Associativiy Arity Precedence Class -B File is a binary file (opposite of -T). None 0 Terms, and list operators (leftward) -M Age of file (at startup) in (fractional) days since modification. Left 2 -> -A Age of file (at startup) in (fractional) days since last access. None 1 ++ --C Age of file (at startup) in (fractional) days since inode change. Right 2 ** Pattern Modifiers 147 Right 1 ! ~ > and unary + and unary /i Ignore alphabetic case distinctions (case insensitive). Left 2 =~ !~ /s Let . match newline and ignore deprecated $* variable. Left 2 */%x /m Let ^ and $ match next embedded \n. Left 2 +-. /x Ignore (most) whitespace and permit comments in pattern. Left 2 << >> /o Compile pattern only once. Right 0,1 Named unary operators None 2 < > <= >= lt gt le ge Additional m// Modifiers 150 None 2 == != <=> eq ne cmp /g Globally find all matches. Left 2 & /cg Allow continued search after failed /g match. Left 2 |^ Left 2 && Additional s/// Modifiers 153 Left 2 || /g Replace globally, that is, all occurences. None 2 .. ... /e Evaluate the right side as an expression.
tr/// Modifiers /c Complement SEARCHLIST. /d Delete found but unreplaced characters. /s Squash duplicate replaced characters.
156
General Regex Metacharacters
159
Symbol Atomic Meaning \…
Varies
…|… (…) […]
No Yes Yes
^
No
. $
Yes No
De-meta next nonalphanumeric character, meta next alphanumeric character (maybe). Alternation (match one or the other). Grouping (treat as a unit). Character class (match one character from a set). True at beginning of string (or after a newline, maybe). Match one character (except newline, normally). True at end of string (or before any newline, maybe).
Regex Quantifiers
159-160
Quantifier Atomic Meaning
No No No No No No
Match 0 or more times (maximal). Match 1 or more times (maximal). Match 0 or 1 time (maximal). Match exactly COUNT times. Match at least MIN times (maximal). Match at least MIN but not more than MAX times (maximal).
No No No {MIN,}? No {MIN,MAX}? No
Match 0 or more times (minimal). Match 1 or more times (minimal). Match 0 or 1 time (minimal). Match at least MIN times (minimal). Match at least MIN but not more than MAX times (minimal).
* + ? {COUNT} {MIN,} {MIN,MAX}
*? +? ??
Extended Regex Sequences Extension
160
Atomic Meaning
(?#…) (?:…) (?imsx-imsx) (?imsx-imsx:…) (?=…) (?!…) (?<=…) (?…) (?{…}) (??{…}) (?(…)…|…) (?(…)…)
No Yes No Yes No No No No Yes No Yes Yes Yes
Comment, discard. Cluster-only parentheses, no capturing. Enable/disable pattern modifiers. Cluster-only parentheses plus modifiers. True if lookahead assertion succeeds. True if lookahead assertion fails. True if lookbehind assertion succeeds. True if lookbehind assertion fails. Match nonbacktracking subpattern. Execute embedded Perl code. Match regex from embedded Perl code. Match with if-then-else pattern. Match with if-then pattern.