I am not sure why you would expect any other behavior?
If the "unless" condition results in the "my my_var" not executing, then 'my_var' is never declared in the scope of the subroutine.
So when the next line assigns to 'my_var' - the assignment is done to a global variable.
So it is not persisting across calls, its just not scoped to the subroutine, but your entire file.
I always thought it was perfectly logical code, and I had used it in some situations as a convenient form of name overloading. Alas it looks like this caught out so many people they made it an error, and now people even claim it was a bug all along... which is nonsense.
In reply to Re: Conditional initialization of lexical (my) variables
by Anonymous Monk
in thread Conditional initialization of lexical (my) variables
by muthm
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |