Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
"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:  <%-{-{-{-<


In reply to Re^3: conditional print. Is correct to use it? (updated) by AnomalousMonk
in thread conditional print. Is correct to use it? by pvaldes

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others wandering the Monastery: (8)
As of 2024-04-18 08:02 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found