Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

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

by AnomalousMonk (Archbishop)
on Oct 28, 2020 at 18:03 UTC ( [id://11123272]=note: print w/replies, xml ) Need Help??


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

... a FALSE value can be "non empty", so || is probably not the best choice.

I don't understand this statement. (Update: hippo has likely shown the way to understanding. :) Is there any false value of $x, empty or not, for which the expression ($x || $y) would not evaluate as the value of $y? Likewise, given the statement
    $x ||= $y;
is there any false initial value of $x for which the final value of $x would not end up as $y?


Give a man a fish:  <%-{-{-{-<

Replies are listed 'Best First'.
Re^3: conditional print. Is correct to use it?
by hippo (Bishop) on Oct 28, 2020 at 21:19 UTC

    I think kcott is saying that you might, in some conditions, wish to retain the non-empty false value in $x rather than discard it in favour of $y. eg. if 0 were a valid input for $x and you had:

    $x = 0; $y = 10; $number = $x || $y; print "$number\n";

    You would see 10 rather than the valid-but-false 0. In such cases $number = $x // $y; might be more appropriate.


    🦛

Re^3: conditional print. Is correct to use it?
by kcott (Archbishop) on Oct 29, 2020 at 03:50 UTC

    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://11123272]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others scrutinizing the Monastery: (3)
As of 2024-03-28 15:40 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found