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

++Indeed. Technically, the no-explicit-return version of the function returns 0 for an empty argument list, so it implictly handles the empty case. (I infer that the OP did the if(@_) just to prevent divide-by-zero.) But I agree that explicit undef is a better return value than relying on a default 0.

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

    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."
      "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.