Anyhow this is sabotaging performance, since calling methods on objects or ties is quite expensive...

Ties aren't cheap compared to variable lookups because they involve a sub call. However, they shouldn't be much slower than a sub call.

Method calls shouldn't be any slower than an sub call because they do the same thing. Even if inheritance is involved, Perl caches the inherited method so it doesn't have to search the inheritance tree for it again.

Of course, there's no reason to use a method call here. $f->() would work just as well.

for my $i ( range(1,9) ) { print $i }

That would take an efficient counting loop (O(1) memory) into an inefficient foreach loop (O(N) memory)

while ( range(1,9) ) {...}

You could make this work by storing the current state of the counter in a per-thread hash indexed by the operator instance. The problem is that you'd have no way of resetting the counter if you previously used last to exit the loop.

On the other hand, the following would be trivial to implement.

for my $i ( 1..9 ) { print $i } for my $i ( range(1,9) ) { print $i } { my $i; while ( range 1,9 => $i ) {...} } { my $iter = range(1,9); while ( $iter->(my $i) ) {...} }
sub range { my $start = shift; my $end = shift; if (!@_) { if (wantarray()) { return $start .. $end; } else { my $i = $start; return sub { return undef if $i > $end; $_[0] = $i++; return 1; }; } } if (defined($_[0])) { ++$_[0]; } else { $_[0] = $start; } if ($_[0] > $end) { return $_[0] = undef; } else { return 1; } }

In reply to Re: Designing a DWIMish interface for generator function by ikegami
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.