Put condition last

xyz if abc instead of if (abc) {xyz}

Replace part of string

substring ($str, 3, 2) = 'replacement'

Swap $a and $b

( $a, $b ) = ( $b, $a );

Read lines from stanrdard input and print them out in sorted order

print sort <>;

Print all the lines containing the word foo

print grep /\bfoo\b/, <>;

Copy all the numbers in @n evenly divisible by 5 into @div5

my @div5 = grep { not $_ % 5 } @n;

Print key-value pairs from %h one per line

foreach my $key ( sort keys %h ) {

    print "$key: $h{$key}\n";

}

Another way to print key-value pairs

print map "$_: $h{$_}\n", sort keys %h;

Rearranging elements in an array

 

@ary[ 1, 3, 5 ] = @ary[ 5, 3, 1 ]

 

Swapping every even and odd element pair in an array

 

# for instance, for a 10 element array the first two elements will evaluate to

# @ary [map {1, 0} 0..5] = @ary

@ary[ map { $_ * 2 + 1, $_ * 2 } 0 .. ( $#ary / 2 ) ] = @ary;

 

Use grep to construct a listwhere the expression evaluates to true

 

@ll = grep (m\pattern\, @l);

 

 

Use of given/when

 

given uses smart matching : given ('xyz') actually means given ($_ ~~ 'xyz')

 

given ($dog) {

when ('Fido') { ... ; continue}

when ('Rover') { ... }

when ('Spot') { ... }

default { ... };

};

can be used in foreach

foreach (@ary) {

when () {...}

when () {...}

}