Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

Re^3: conditional print. Is correct to use it?

by kcott (Archbishop)
on Oct 29, 2020 at 03:50 UTC ( [id://11123288]=note: print w/replies, xml ) Need Help??


in reply to Re^2: conditional print. Is correct to use it?
in thread conditional print. Is correct to use it?

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>"' <>

— Ken

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://11123288]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others chilling in the Monastery: (3)
As of 2024-04-19 23:37 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found