if (!$variable1) {
$variable1 = 'default';
}
unless (defined($variable2)) {
$variable2 = 'default';
}
can be done much more quickly as$variable1 ||= 'default'; # true or $variable2 //= 'default'; # defined ori prefer the second
When I started this blog, my intent was to illustrate different ways to do things in Perl (where TIMTOWTDI originated). Since then I have transitioned from Perl to Python, become active in several open source software projects, and started to learn how to contribute to project code in addition to test automation. I am returning to the blog to discuss things as I learn them. 'There's More Than One Way To Do It' remains true, even using different languages or an established design pattern.
if (!$variable1) {
$variable1 = 'default';
}
unless (defined($variable2)) {
$variable2 = 'default';
}
can be done much more quickly as$variable1 ||= 'default'; # true or $variable2 //= 'default'; # defined ori prefer the second
sub my_function {
my $val1 = shift();
my $val2 = shift();
my $val3 = shift();
...
}
does the same thing assub my_function {
my $val1 = @_->[0];
my $val2 = @_->[1];
my $val3 = @_->[2];
...
}
but my favorite is sub my_function {
my ( $val1, $val2, $val3 ) = @_;
...
}