mtroost has asked for the wisdom of the Perl Monks concerning the following question:
Testing this code byuse strict; use warnings; my $i = 1; #return a pointer to a function that should be invoked sub makeLazy { my ($inp) = @_; sub { eval $inp; }; } my $part2 = makeLazy('print "\nnow its $i";'); print "\ni is ".$i++; $part2->(); print "\ni is $i";
Hurray! It *seems* to work alright. But what happens when i move the declaration of $i?% test1.pl i is 1 now its 2 i is 2
Lets give it another testrun:use strict; use warnings; #return a pointer to a function that should be invoked sub makeLazy { my ($inp) = @_; sub { eval $inp; }; } my $i = 1; my $part2 = makeLazy('print "\nnow its $i";'); print "\ni is ".$i++; $part2->(); print "\ni is $i";
Eeks! Nothing happens, the $part2->() does nothing and return nothing. It doesnt even eval $i to nothing, it just does not execute! What is the cause? What is a workaround?% test1.pl i is 1 i is 2
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Not-so-lazy evaluation?
by Joost (Canon) on Nov 06, 2007 at 15:50 UTC | |
by mtroost (Initiate) on Nov 06, 2007 at 16:33 UTC | |
by dave_the_m (Monsignor) on Nov 06, 2007 at 17:00 UTC | |
by Joost (Canon) on Nov 06, 2007 at 17:58 UTC | |
|
Re: Not-so-lazy evaluation?
by dragonchild (Archbishop) on Nov 06, 2007 at 15:51 UTC | |
by mtroost (Initiate) on Nov 06, 2007 at 16:39 UTC | |
by dragonchild (Archbishop) on Nov 06, 2007 at 17:30 UTC | |
|
Re: Not-so-lazy evaluation?
by johngg (Canon) on Nov 06, 2007 at 15:51 UTC |