in reply to Re: Referencing localized variables, and typeglobs
in thread Referencing localized variables, and typeglobs

Thanks for the reply! My main concern is that I haven't yet found a description of the behavior in the perldocs or (so far) elsewhere.

So while you are right that the behavior does seem logical, there is another possible interpretation of the code: One might expect that after the effects of the local are over, $x and $y could refer back to the original %h, instead of some anonymous hashes. I'm not saying this interpretation is better or worse than the actual behavior, just that it'd be nice if it were documented.

Another concern is that perlsub says "This operator works by saving the current values of those variables in its argument list on a hidden stack and restoring them upon exiting the block, subroutine, or eval." - So the values are saved on a stack and presumably the temporary value is popped back off when the scope exits. Without some reassurance from the docs, one might worry that the temporary variables might somehow "go away" when the scope exits (e.g. is this stack refcounted?) and a reference to such a value might become invalid (however unreasonable or not the worry might be).

Replies are listed 'Best First'.
Re^3: Referencing localized variables, and typeglobs
by Laurent_R (Canon) on Aug 30, 2017 at 21:38 UTC
    One might expect that after the effects of the local are over, $x and $y could refer back to the original %h,
    Why should it? $x and $y are assigned to the value returned by the do block. And this value happens to be an anonymous hash ref produced within the block. And $x does not know anything about the %h hash. The fact that %h is restored to an empty hash immediately thereafter is irrelevant to the value acquired by $x at the time of the assignment.

    Well, I understand your concern, but I do not think there is any reason to worry here. I think the behavior is quite clear.