Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

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

by pvaldes (Chaplain)
on Oct 27, 2020 at 21:45 UTC ( [id://11123237]=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?

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)

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

Replies are listed 'Best First'.
Re^3: conditional print. Is correct to use it? (updated)
by AnomalousMonk (Archbishop) on Oct 28, 2020 at 19:29 UTC
    "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
Domain Nodelet?
Node Status?
node history
Node Type: note [id://11123237]
help
Chatterbox?
and the web crawler heard nothing...

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

    No recent polls found