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
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |