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

Technically, the no-explicit-return version of the function returns 0 for an empty argument list

I am aware of that, but they are relying undefined behaviour to do so[1], and it's a poor value to return.

I infer that the OP did the if(@_) just to prevent divide-by-zero.

That would actually have been a far better outcome!


  1. "If no return is found and if the last statement is an expression, its value is returned. If the last statement is a loop control structure like a foreach or a while , the returned value is unspecified. The empty sub returns the empty list."

Replies are listed 'Best First'.
Re^5: resetting a foreach loop!
by AnomalousMonk (Archbishop) on Nov 17, 2017 at 20:15 UTC
    "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:  <%-{-{-{-<

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