in reply to Re^8: OUR declaration
in thread OUR declaration

I know that the actually memory is not freed, but that's an implementation detail. The behavior is not like a global. Lexical variables don't retain values when they go out of scope.

I don't think the author was trying to make $q a global. I think the author was trying to fix a closure problem, and "local our" is not as good a solution to that problem as passing the variables to the subs.

Replies are listed 'Best First'.
Re^10: OUR declaration
by ikegami (Patriarch) on Sep 25, 2006 at 16:56 UTC

    Who said anything about making $q a global? It already was. I'm saying he might have wanted it to keep on being a global. When fixing someone else's code, I try to follow the "minimal change" doctrine. If it's a global, leave it a global. Especially since it's proper for this variable to be a global, given how it's used for input, processing and output.

    I'm still confused why you say "Lexical variables don't retain values when they go out of scope". It's true, but it's meaningless here. Both lexical variables and localized package variables lose their values at the end of their scope. Both global lexical variables and global package variables have the same scope. Replacing my $q with local our $q would give $q neither a larger scope nor a greater persistance. Do you think otherwise?

      The scope of a localized package variable variable is not really the same as a lexical, and the existence of that variable is persistent in a way that a lexical is not, even if the value becomes undefined. That's not really the point though. The point is that this is a bad solution. Changing the code so that subs are called with the variables they need (providing isolation they don't have when used with globals) is a good solution. Bad "quick fix" solutions should not be encouraged. Think of the poor guy who comes across this code next and how his eyes will bug out at the "local our" junk.