in reply to Re^2: Not-so-lazy evaluation?
in thread Not-so-lazy evaluation?
Basically that opinion shows faulty but questions still remains. When is $i evaled? Or in what dictionary/environment/whatever-mechanism-is-used?
In your case, $i is evaluated when the string containing a literal '$i' is eval()ed. You could evaluate $i earlier by interpolating $i into the string (evaluating $i when the string is created), but then, that would convert the value of $i into a string, which isn't always what you want:
Note that this also decouples the eval'd code in makeLazy from the actual current value of $i.makeLazy(qq(print "\nnow its $i"));
Because of the same problem i run into with my code (but i didnt knew until now that it was the same problem): if you defer the declaration of $i then you get Global symbol "$i" requires explicit package nameWell, what do you want it to do? Either you want a lexical variable, but then you must declared it in its lexical scope, or you may want a (possibly local()ized package variable) - which you don't have to declare, if you prepend them with the package name (or you *could* switch of strict 'vars').
update: Note that in my alternative version, using my $defer = sub { something_with($i) } syntax, $i is evaluated when $defer->() is called, but the scoping of $i is resolved at compile time, and figuring out the actual instance of $i may be resolved when the $defer = .. assignment is executed (that last part is important if you're generating these $defer closures from within another subroutine, with $i declared lexical in the outer subroutine).
You may find this column useful
|
|---|