Is there a way to get Perl to call FETCHSIZE on a bound array before every FETCH call? My linked array knows the maximum size, but may decrease from this size depending on the results of previous FETCH calls. here is a contrived example that filters the list only to even items with a lazy rating:
use warnings; use strict; package VarSize; sub TIEARRAY { bless $_[1] => $_[0] } sub FETCH { my ($self, $index) = @_; splice @$self, $index, 1 while $$self[$index] % 2; $$self[$index] } sub FETCHSIZE {scalar @{$_[0]}} my @source = 1 .. 10; tie my @output => 'VarSize', [@source]; print "@output\n";
for brevity, I omitted a bunch of error checking code (for example, how to handle calls starting from an index other than 0)
EDIT: instead of the two above print statements, if one of the following two lines is used, the first will work fine, the second will give warnings.
print "$_ " for @output;
Update:
The actual module that implements the variable-sized array is called List :: Gen , which is located on CPAN. A filter function that behaves like grep but works with List::Gen lazy generators. Does anyone have any ideas that could make the filter implementation better?
(the test function is similar, but returns undef in broken slots, keeping the array size constant, but, of course, has different usage semantics than grep )
source share