M3 dor - markov2/perl5-Mail-Box GitHub Wiki
Defined-or '//'
You shall be careful with Perl's idea about booleans. Real bools to not exist, but every value also has a true or false attached to it.
For completeness, here the table
undef false undefined / uninitialized
"" false empty string
0 false zero, nothing
@x false when empty
"0" false(!) the string zero
0.0 false float 0, dangerous wrt rounding errors
A common sloppiness is a sleeping bug:
$filename or die "No filename given.\n";
This usually works, but produces an error when the filename is "0". So, the correct code would be
defined $filename or die "No filename given.\n";
More difficult is this case, with a common way to express fallback values:
my $pay = $action_price || $normal_price || die "No price";
Is a price of '0' ever acceptable? It will not be picked-up. So, you may need to write:
my $pay = defined $action_price ? $action_price : defined $normal_price ? $normal_price : die;
Perl 5.10 introduced "//", the defined-or. In this case, only an undefined value is considered false:
my $pay = $action_price // $normal_price // die "No price";
Patches
Exists
Meanwhile, 4 places where fixed where boolean parameters were not correctly processed. The following accepts only non-undef versions of false as boolean:
my $take = defined $args{take} ? $args{take} : 1;
Corrected into:
my $take = exists $args{take} ? $args{take} : 1;