Which of these subroutines is not like the other?
sub or1 {
my ($a,$b) = @_;
return $a || $b;
}
sub or2 {
my ($a,$b) = @_;
$a || $b;
}
sub
What rules of thumb do you use to decide which construct to use and make sure the code is doing what you think it is doing
The operator precedence rules.
||
binds tightly, or
binds weakly. There is no "rule of thumb".
If you must have a rule of thumb, how about "only use or
when there is no lvalue":
or
:
open my $fh, '>', 'file' or die "Failed to open file: $!"
||
:
my $greeting = greet() || $fallback || 'OH HAI';
I agree with MJD about avoiding parens; if you don't know the rules, look them up... but don't write (open(my $fh, '>', 'file')) or (die("Failed to open file: $!"))
"just to be sure", please.