As I already admitted last is really an unexpected problem... 8(
(Hmm ... maybe there's a way by trying to catch the destruction of the iterator variable... )
Talking about positional subs and leaking I already found a nice workaround by using code-blocks!¹ 8)
sub noleak (&$) { my $cr=shift; $_[0]=$cr } my @a; for (0..9) { noleak {0,5} my $a; push @a,$a; print $a }
The code-reference seems - as expected - to be precalculated at compile time and is fix!
/usr/bin/perl -w /home/lanx/B/PL/PM/iter_leak.pl CODE(0xa099b48)CODE(0xa099b48)CODE(0xa099b48)CODE(0xa099b48)CODE(0xa09 +9b48)CODE(0xa099b48)CODE(0xa099b48)CODE(0xa099b48)CODE(0xa099b48)CODE +(0xa099b48)
Or do you have an idea to break this approach?
Cheers Rolf
UPDATE:
ARGHH it only works when using fixed values...
sub noleak (&$) { my $cr=shift; $_[0]=$cr } my ($start,$end)=(1,20); my @a; for (0..9) { $start++; noleak {$start,$end} my $a; push @a,$a; print $a }
output
CODE(0x8e29760)CODE(0x8e47ca8)CODE(0x8e47d08)CODE(0x8e47d68)CODE(0x8e4 +7dc8)CODE(0x8e47e28)CODE(0x8e47e88)CODE(0x8e47ee8)CODE(0x8e47f48)CODE +(0x8e47fa8)
... but commenting out the push produces:
CODE(0x81f7760)CODE(0x81f7760)CODE(0x81f7760)CODE(0x81f7760)CODE(0x81f +7760)CODE(0x81f7760)CODE(0x81f7760)CODE(0x81f7760)CODE(0x81f7760)CODE +(0x81f7760)
and there is no need to propagate the coderef outside the sub...
UPDATE
¹) I ran some tests, better forget it...it really only seems to work reliably with constant values in the code-block ...seems to be an optimization issue.
UPDATE I'm going to run some tests with state.
In reply to Re^6: Designing a DWIMish interface for generator function
by LanX
in thread Designing a DWIMish interface for generator function
by LanX
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |