in reply to Re^7: Confusion over scope of "my" vars
in thread Confusion over scope of "my" vars

> To tie it back to the OP, nothing happens on scope exit if the my isn't executed.

Do you know if this is a well defined feature?
Can't see why undefining a var with minimized ref-counter should be restricted to a previous my-init !(?)

Cheers Rolf

  • Comment on Re^8: Confusion over scope of "my" vars

Replies are listed 'Best First'.
Re^9: Confusion over scope of "my" vars
by ikegami (Patriarch) on Nov 27, 2008 at 19:23 UTC

    Do you know if this is a well defined feature?

    We've already convered this. It's documented to be undefined behaviour.

    Can't see why undefining a var with minimized ref-counter should be restricted to a previous my-init !(?)

    But you're ok with conditionally declaring variables?

    Anyway, there are tWo problems with what yousaid:

    • The variables are cleared, not undefined. The string buffer is reused too.
    • It has nothing to do with the assignment ("init"). my $var if 0; would be just as problematic.

    In order to reuse the variable, the function needs a reference to it (so it can find it) and the refcount needs to stay above zero (in order for it to be around to be reused). That means the refcounting mechanism won't help us here.

    I don't know why they tied it to the execution of my rather than the compilation of my, but it might have to do with saving opcodes. Or simplfying the compiler. Or maybe it would make subroutine reentry too complicated to do otherwise.