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

Re: conditional print. Is correct to use it?

by kcott (Archbishop)
on Oct 28, 2020 at 06:42 UTC ( [id://11123251]=note: print w/replies, xml ) Need Help??


in reply to conditional print. Is correct to use it?

G'day pvaldes,

In terms of style, I'd say it's perfectly acceptable to embed an expression in a print list. Having said that, if the expression is long or complicated, I'd usually move it out of the print statement.

$fo ||= 'Roses are red ...'; print "My homework: $fo";

As others have already pointed out, a FALSE value can be "non empty", so || is probably not the best choice.

# (fo will be updated with a real value in this part)

You'll need to tell us what's going on there. My first guess was that you're perhaps doing something like:

$ perl -E ' my $fo = ""; update($fo); print "|$fo|"; sub update { my ($arg) = @_; $arg .= "XXX"; } ' ||

when what you really wanted was something like:

$ perl -E ' my $fo = ""; update(\$fo); print "|$fo|"; sub update { my ($arg) = @_; $$arg .= "XXX"; } ' |XXX|

But there would be dozens of reasons why your update could be returning a "forrible" value. :-)

— Ken

Replies are listed 'Best First'.
Re^2: conditional print. Is correct to use it?
by AnomalousMonk (Archbishop) on Oct 28, 2020 at 18:03 UTC
    ... 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:  <%-{-{-{-<

      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.


      🦛

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

How do I use this?Last hourOther CB clients
Other Users?
Others about the Monastery: (6)
As of 2024-04-24 11:52 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found