in reply to Re^2: Eval doesn't see lexicals
in thread Eval doesn't see lexicals
I take it that variables still hang around at the start of a new package even though they are no longer directly acessible ( I am supposing that putting { and } around the entire package would change this, at least the samples for Class::Std do it, and there would have to be a reason.)No, source code files are already treated as if they were surrounded by { and }, and variables are normally freed at the end of executing the included file. If a sub in that file captures the variable, then its lifetime is extended, but only that sub can subsequently access the retained value. For example:
Note that the variable's lifetime is extended merely by the existence of b(), but even with the existence of b(), a() cannot see the variable.# Foo.pm my $var = 1; sub a { eval '$var' # sees an undefined value when later called } sub b { $var; eval '$var'; # sees 1 because b() has captured $var }
Dave.
|
|---|