PERL
This is intended as a personal on-line resource of perl code that I've found usefull. As time goes on, and I get more experience, I'll add more to this page. More extensive on-line crib-sheets can be found at TODO. Personally I tend to prefer to have references examples of actual code.
The keyword BLOCK refers to a generic block of perl code.
/^/#/
Flow control
conditional
if ( $a != $b ) { BLOCK }
elsif($b = 3) { BLOCK }
else { BLOCK }
unless ( $a >= $b && $a < 0) { BLOCK }
looping
LOOPLABEL: while ( $a != $b ) {
if ( $c = 0 ) { last LOOPLABEL} # exits the loop
if ( $d = 2 ) { next LOOPLABEL} # jumps to next itteration of the loop
$c--;
}
for ($a=0; $a++; $a>10) {BLOCK
}
CONDITIONAL
Numeric
- = < > <= >= != - for numeric.
String
- eq ne - for equality.
- lt le gt ge - for lexical comparison.
- cmp - returns -1 if $left lt $right, 0 if $lef eq $right, 1 of $left gt $right.
- =~ !~ - for pattern matching.
pattern matching
- . - any character
- \s - any white space character
- \S - any non-white space character
- \w - any word character (aphanumeric or underscore
- \W - any non-word character
- \d - any numeric character
- \D - any non-white space character
pattern count suffix
- ? - 0 or 1 of the previous character
- * - 0 or more of the previous character
- + - 1 or more of the previous character
concatinalting conditionals
evaluated left to right.
- && - "and, if true continue to the next test"
- || - "and, if false continue to the next test"
Strings
- \n - newline
- \t - tab
- \b - backspace
- \r - carriage return
- \e - escape character
- \a - alert
- \f - from feed
operators
- . - concatinate strings
- x - duplicate "me" x 3 becomes "mememe"
- lc uc lcfirst ucfirst - case assertions
- sprintf - TODO
- index rindex - finds first/next and last occurance of string.
Lists
Hashes
GVIM maps
-
:map <F2> ^y$i<<ESC>$a></<ESC>pa><ESC>bba
# expands word to open and close HTML tag.
-
:map <F3> :s/&/&/ge<ESC>:s/</\</ge<ESC>:s/>/\>/ge<ESC>
# converts & < and > to HTML equivalent.
prototypes
-
map <S-F3> :s/\</</ge<ESC>:s/\>/>/ge<ESC>:s/&/&/ge<ESC>
map <S-F3> :s/</</ge<ESC>:s/>/>/ge<ESC>
map :s/</:s/>/>/ge