Hi

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

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.