Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

Re^7: resetting a foreach loop!

by AnomalousMonk (Archbishop)
on Nov 18, 2017 at 00:43 UTC ( [id://1203714]=note: print w/replies, xml ) Need Help??


in reply to Re^6: resetting a foreach loop!
in thread resetting a foreach loop!

... if statements ... aren't expressions ...

if statements aren't expressions, but an
    if (CONDITION) { ... }
statement must evaluate the  CONDITION expression, and if that's the last expression evaluated in a subroutine (because it evaluates false), I thought that's what must be returned. At least, whenever I've encountered this situation, that's the way it's worked. (I don't like implicit returns myself and never use them, but just sayin' ...) I'd be interested to learn more about this.


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

Replies are listed 'Best First'.
Re^8: resetting a foreach loop!
by pryrt (Abbot) on Nov 18, 2017 at 02:11 UTC

    Yes, that appears to be what's happening:

    C:>perl -le "sub u { unless(shift) { 'false' } }; print u($_) for unde +f, '', 0..3" false false false 1 2 3 C:>perl -le "sub i { if(shift) { 'true' } }; print i($_) for undef, '' +, 0..3" 0 true true true

    I'm not sure if it's meeting the technical definition of "expression" from "If no return is found and if the last statement is an expression, its value is returned"... but the last statement executed was the failed if or unless, and as part of that execution of that statement, the CONDITIONAL expression had to be evaluated, so I can definitely see a good case for why that EXPR's value is what is returned. But since I couldn't find a perl-specific definition of "expression" (and it appears it's been a longstanding lack of definition, because as far back as 2001, people were asking What is the difference between a Statement and an Expression? without getting a canonical answer from the perl documents), there is wiggle-room in the interpretation of whether the documents claim the return value is unspecified or not.

    Interesting sub-discussion. Thanks!

      Now that I've thought about it some more, I think the point ikegami is making is that just the fact that a behavior is commonly implemented doesn't mean it is actually specified anywhere. And in this case, the behavior of  if (CONDITION) BLOCK is nowhere specified, so don't rely on it.

      As I've mentioned before and as you've noted, the behavior of an if-statement returning the result of evaluating  CONDITION when  CONDITION is false and the if-statement is the final statement executed in a subroutine is familiar and seems quite reasonable; it's not surprising that many, perhaps most, Perl implementations adopt this behavior. Whether this behavior is specified and reliable is another question, one I don't have time to pursue ATM. Other questions occur: what's returned from evaluation of a false  CONDITION in a while-loop that is the last statement executed in a subroutine? (I don't dare think about what's going on with a for-loop!)

      Food for thought — and links for more reading.


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

How do I use this?Last hourOther CB clients
Other Users?
Others having a coffee break in the Monastery: (5)
As of 2024-04-19 10:28 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found