Pretty much what ++hippo said. :-)
length will return FALSE for both '' and undef,
and TRUE for both 0 and '0'.
It's first documented as doing that in 5.12.0;
however, there was a bug that was fixed in 5.14.0
(perl5140delta: Syntax/Parsing Bugs)
so I'd be more comfortable with both defined and length
if using anything earlier than 5.14.0.
# 5.14.0 or later
$ perl -E 'my ($x, $y) = (0, "fallback"); $x = length $x ? $x : $y; sa
+y $x'
0
$ perl -E 'my ($x, $y) = ("0", "fallback"); $x = length $x ? $x : $y;
+say $x'
0
$ perl -E 'my ($x, $y) = ("", "fallback"); $x = length $x ? $x : $y; s
+ay $x'
fallback
$ perl -E 'my ($x, $y) = (undef, "fallback"); $x = length $x ? $x : $y
+; say $x'
fallback
# 5.12.0 or earlier
$ perl -E 'my ($x, $y) = (0, "fallback"); $x = (defined $x && length $
+x) ? $x : $y; say $x'
0
$ perl -E 'my ($x, $y) = ("0", "fallback"); $x = (defined $x && length
+ $x) ? $x : $y; say $x'
0
$ perl -E 'my ($x, $y) = ("", "fallback"); $x = (defined $x && length
+$x) ? $x : $y; say $x'
fallback
$ perl -E 'my ($x, $y) = (undef, "fallback"); $x = (defined $x && leng
+th $x) ? $x : $y; say $x'
fallback
# Probably not what was intended
$ perl -E 'my ($x, $y) = ("0", "fallback"); $x ||= $y; say $x'
fallback
$ perl -E 'my ($x, $y) = ("", "fallback"); $x //= $y; say "<$x>"'
<>
|