Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

Re: conditional print. Is correct to use it?

by davido (Cardinal)
on Oct 27, 2020 at 15:25 UTC ( #11123210=note: print w/replies, xml ) Need Help??


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

Is your question about style, or about behavior? There's nothing surprising to me about the behaviors you're describing, but maybe there's an expectations vs reality gap.

You can use Devel::Peek to inspect the actual contents of a scalar.

You can use perldata to learn about what Perl sees as truth and falsehood.

You can refer to perlop to see the precedence list, where || is lower than +.

You can use perl -MO=Deparse,-p,-x9 -e 'print "we have ", ($fo + 2 || 8), " beans";' to see how that parses, and how precedence rules apply. That last one produces:

perl -MO=Deparse,-p,-x9 -e 'print "we have ", ($fo + 2 || 8), " beans" +;' print('we have ', (($fo + 2) || 8), ' beans'); -e syntax OK

Dave

Replies are listed 'Best First'.
Re^2: conditional print. Is correct to use it?
by pvaldes (Chaplain) on Oct 27, 2020 at 21:45 UTC

    Is more about style. I'm in the middle of an iteration. The known route would be:

    put an if loop before -> to fix $variable -> and then print "something $variable something"... to a file.

    I'm exploring to include the loop directly inside the print line. Start printing "something" to a file -> then include a mini loop to check the current status of the variable and then finish printing "something".

    what strategy would be more correct in your opinion? "if(true){a} else {b}" or just (a||b)

    (are both equally correct?)

    Note: (About why not write just: my $fo; I have a bunch of variables to initialize. Some are expected to be numeric, other will receive a chain. I want to remind who is who. This is the reason to express specifically the empty chain here. (i.e: my $fo = q{}; my $bar = 0;... instead to just write: my ($fo, $bar)

      "if(true){a} else {b}" or just (a||b)

      (are both equally correct?)

      A statement block like if(true){a} else {b} does not evaluate to any value (but see Note 1 below), but an expression like (a||b) always does. Therefore, IMHO it's not correct to say these are equivalent or "equally correct." The if/else-statement block can be made to have an effect equivalent to the expression, but only if the a b expressions or statement(s) have the correct side effects.

      An expression can always be incorporated into another expression — if precedence is handled properly! A statement cannot be incorporated into another statement unless you contort your program logic with some sort of usually unnecessary eval nonsense. E.g.:

      Win8 Strawberry 5.8.9.5 (32) Wed 10/28/2020 15:06:58 C:\@Work\Perl\monks >perl -Mstrict -Mwarnings my $x; # undefined/false my $y = 'default string'; my $side; if (not $x) { $side = $y; } # true clause has side effect # $side = $y unless $x; # a more terse alternative printf "side effect '%s' logical-or '%s' ternary '%s' \n", $side, ($x || $y), $x ? $x : $y; ^Z side effect 'default string' logical-or 'default string' ternary 'de +fault string'

      Notes:

      1. An if- or if/else-statement block like
            if (CONDITION) { statements(s)  }
        or
            if (CONDITION) { statements(s)  } else { statements(s) }
        will return a value from a subroutine if it is the last thing executed in the subroutine, but this behavior is inherent to the behavior of subroutines, and not directly related to the behavior of conditional statement blocks.

        Update: To be more precise, what is returned from a subroutine that has no explicit return statement is the value of the last statement or expression executed in the subroutine. So in the subroutine
        sub func { ... ... if ($x) { foo(); bar(); } else { fee(); fie(); } }
        the return value of bar() is returned by func() if $x is true, that of fie() if $x is false.
        Win8 Strawberry 5.8.9.5 (32) Wed 10/28/2020 22:49:24 C:\@Work\Perl\monks >perl -Mstrict -Mwarnings printf "'%s' returned from true clause \n", func(1); printf "'%s' returned from false clause \n", func(); sub func { my $x = shift; printf "x is %-7s", $x ? 'true' : 'false'; if ($x) { foo(); bar(); } else { fee(); fie(); } } sub foo { return 'from ' . (caller 0)[3]; } sub bar { return 'from ' . (caller 0)[3]; } sub fee { return 'from ' . (caller 0)[3]; } sub fie { return 'from ' . (caller 0)[3]; } ^Z x is true 'from main::bar' returned from true clause x is false 'from main::fie' returned from false clause


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

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others romping around the Monastery: (7)
As of 2021-01-23 21:40 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Notices?