Jim Cheung

Perl

Scalar

unicode support, add use utf8;

string repetition: "foo" x 3 # foofoofoo

use warnings; perl -w my_program

use diagnostics; perl -Mdiagnostics my_program

print "fred ate ${what}s.\n"

'', '0' and 0 are both false. ('0' is the ONLY non-empty string that is false)

check undef: defined($variable)
normally, <STDIN> return undef as end-of-file

List and Array

access last element: $list[$#list] or $list[-1]

literal: (1, 2, 3)

range: (1..10), range counts up by 1, (1.2..10.5) will be the same. range only counts up, use reverse to produce a list which counts down.

qw(apple banana orange), words in side qw were treated as inside single-quote. separated by white-spaces, tabs or new lines. and you can choose a different delimiter like qw/apple banana orange/

list assignment: ($a, $b, $o) = qw(apple banana orange)

swap values: ($a, $b) = ($b, $a), right side is evaluated first for =

copy a list: @copy = @original

Operators:

@array = 5..9;
$fred = pop(@array); # fred gets 9, @array now has (5, 6, 7, 8)
pop(@array); # @array now has (5, 6, 7), 8 is discarded
push(@array, 8); # @array now has (5, 6, 7, 8)
push(@array, 9..18); # @array = (5..18)
$m - shift(@array); # $m gets 5, which is removed from @array
unshift(@array 5); # @array gets 5 on head of list
unshift(@array 1..4); # @array = (1..18)
@removed = splice(@array, 2); # index counts from 0, removes 4..18 from @array, which assigns to @removed
@removed2 = splice(@removed, 1, 2); # (5, 6) assigns to @removed2, @remove becomes (4, 7..18)
@removed3 = splice(@removed, 0, 2, 1..7); # @removed3 = (4, 7), replace 1..7 on index 0, @removed becomes 1..18

Books