Monday, July 18, 2011

there is more than one way to make sure a variable has a value

if (!$variable1) {
    $variable1 = 'default';
}

unless (defined($variable2)) {
    $variable2 = 'default';
}
can be done much more quickly as
$variable1 ||= 'default'; # true or
$variable2 //= 'default'; # defined or
i prefer the second

Friday, July 15, 2011

there is more than one way to access the parameters of the method you just called

sub my_function {
    my $val1 = shift();
    my $val2 = shift();
    my $val3 = shift();

    ...

}
does the same thing as
sub my_function {
    my $val1 = @_->[0];
    my $val2 = @_->[1];
    my $val3 = @_->[2];

    ...

}
but my favorite is
sub my_function {
    my ( $val1, $val2, $val3 ) = @_;

    ...
}