in reply to Re^2: why need my in a foreach loop?
in thread why need my in a foreach loop?

the subroutine is a closure

Backwards. It works because the subroutine doesn't capture package variables. If you switched our for my in my snippet, you'd have a closure, and you'd get a different result.

Replies are listed 'Best First'.
Re^4: why need my in a foreach loop?
by 7stud (Deacon) on Dec 02, 2010 at 03:46 UTC

    Nice catch.

    If you switched our for my in my snippet, you'd have a closure, and you'd get a different result.

    Is that correct? Because the my variable would still be in scope, would the subroutine qualify as a closure? It's my understanding that a subroutine is a closure when it preserves a variable that has gone out of scope.

      I don't know the precise definition if there is one. For practical purposes, I find it doesn't matter.

      Correction: In this case, the sub truly acts as a closure. If it didn't, it wouldn't matter if you used our or my.