in reply to Re: next'ing out of a "foreach" loop within a subroutine
in thread next'ing out of a "foreach" loop within a subroutine

Yes, I see why it's a bad idea.

So the "last" ends the foreach loop since a subroutine returns by default the result of the last operation right?
  • Comment on Re^2: next'ing out of a "foreach" loop within a subroutine

Replies are listed 'Best First'.
Re^3: next'ing out of a "foreach" loop within a subroutine
by pg (Canon) on Oct 30, 2005 at 04:30 UTC
    "So the "last" ends the foreach loop since a subroutine returns by default the result of the last operation right?"

    The result of the last operation is not the last operation, right? So this does not provide an answer.

    If the last statement comes without a LABEL, it ends the innermost enclosing loop. Semantically, a sub is a block, and in the demo code, the innermost enclosing loop is that foreach loop.

    It is just the way the language was designed. You can argue that this should never happen, and the last statement inside a sub should not impact any loop outside the sub.

Re^3: next'ing out of a "foreach" loop within a subroutine
by ysth (Canon) on Oct 30, 2005 at 05:56 UTC
    So the "last" ends the foreach loop since a subroutine returns by default the result of the last operation right?
    No. If you use next or last to exit a subroutine, the subroutine doesn't return a value; the next code to execute will a new iteration of the loop or the code after the loop, not the code receiving whatever the sub would have returned if it had returned.