Beefy Boxes and Bandwidth Generously Provided by pair Networks
Come for the quick hacks, stay for the epiphanies.
 
PerlMonks  

Re^5: resetting a foreach loop!

by AnomalousMonk (Archbishop)
on Nov 17, 2017 at 20:15 UTC ( [id://1203708]=note: print w/replies, xml ) Need Help??


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

"If no return is found [and] the last statement is a loop control structure like a foreach or a while , the returned value is unspecified. ..."

Does this apply to if-blocks? I don't think of if as a "loop control" structure.


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

Replies are listed 'Best First'.
Re^6: resetting a foreach loop!
by ikegami (Patriarch) on Nov 18, 2017 at 00:07 UTC

    Even if it doesn't, it's still unspecified for if statements as they aren't expressions either.

      ... 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:  <%-{-{-{-<

        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!

Log In?
Username:
Password:

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

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

    No recent polls found