Perl Quick Reference Card
Operator Precedence (continued) 87 Associativiy Arity Precedence Class version 0.02 – editor: John Bokma – freelance programmer Right 3 ?: DRAFT VERSION, check: http://johnbokma.com/perl/ Right 2 = += -= *= and so on Backslashed Character Escapes 61 Left 2 , => \n Newline (usually LF) \0 Null character (NUL) Right 0+ List operators (rightward) \r Carriage return (usually CR) \033 ESC in octal Right 1 not \t Horizontal tab (HT) \x7f DEL in hexadecimal Left 2 and \f Form feed (FF) \cC Control-C Left 2 or xor \b Backspace (BS) \x{263a} Unicode, ☺(smiley) \a Alert (BEL) \N{NAME} Named character File Test Operators 98 \e Escape (ESC) -r File is readable by effective UID/GID. -w File is writable by effective UID/GID. Translation Escapes 61 -x File is executable by effective UID/GID. \u Force next character to uppercase (“titlecase” in Unicode). -o File is owned by effective UID/GID. \l Force next character to lowercase. -R File is readable by real UID/GID. \U Force all following characters to uppercase -W File is writable by real UID/GID. \L Force all following characters to lowercase -X File is executable by real UID/GID. \Q Backslash all following non-"word" characters (quotemeta) -O File is owned by real UID/GID. \E End \U, \L, or \Q. -e File exists. Quote Constructs 63 -z File has zero size -s File has nonzero size (returns size). Customary Generic Meaning Interpolates -f File is a plain file. -d File is a directory. '' q// Literal string No -l File is a symbolic link. "" qq// Literal string Yes -p File is a named pipe (FIFO). `` qx// Command execution Yes -S File is a socket. () qw// Word list No -b File is a block special file. // m// Pattern match Yes -c File is a character special file. s/// s/// Pattern substitution Yes -t Filehandle is open to a tty. y/// tr/// Character translation No -u File has setuid bit set. "" qr// Regular expression Yes -g File has setgid bit set. -k File has sticky bit set. Note: no interpolation is done if you use single quotes for delimiters. -T File is a text file. Operator Precedence 87 -B File is a binary file (opposite of -T). Associativiy Arity Precedence Class -M Age of file (at startup) in (fractional) days since modification. None 0 Terms, and list operators (leftward) -A Age of file (at startup) in (fractional) days since last access. Left 2 -> -C Age of file (at startup) in (fractional) days since inode change. None 1 ++ -Pattern Modifiers 147 Right 2 ** /i Ignore alphabetic case distinctions (case insensitive). Right 1 ! ~ > and unary + and unary /s Let . match newline and ignore deprecated $* variable. Left 2 =~ !~ /m Let ^ and $ match next embedded \n. Left 2 */%x /x Ignore (most) whitespace and permit comments in pattern. Left 2 +-. /o Compile pattern only once. Left 2 << >> Right 0,1 Named unary operators Additional m// Modifiers 150 None 2 < > <= >= lt gt le ge /g Globally find all matches. None 2 == != <=> eq ne cmp /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. Left 2 || /e Evaluate the right side as an expression. None 2 .. ...
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.