in reply to Re^10: How to completely destroy class attributes with Test::Most?
in thread How to completely destroy class attributes with Test::Most?
Ok, I spent a lot more time on this. I got it back to a one step process while using your idea to create iterator objects AND can do multiple iterators now. Woot!
sub AUTOLOAD { our $AUTOLOAD; my $s = shift; $AUTOLOAD =~ /.*::get(_next)*(_\w+)_files*$/ or croak "No such method: $AUTOLOAD"; my ($next, $type) = ($1, $2); # Handle edge case when get_next_file() method is called if (!$next && $type eq '_next') { $next = 1; $type = ''; } # return the requested list of files if (!$next) { my $attr = "${type}_files"; my @files = @{$s->{$attr}}; return @files; } # get a single file from an iterator if ($s->{"_iterator_$type"}) { my $file = $s->{"_iterator_$type"}->next_file; $s->{_last_iterator_file} = $file; undef $s->{"_iterator_$type"} if (!$file); return $file; } else { my @files = $type ? @{$s->{"${type}_files"}} : $s->get_files; return '' if !@files; my $class = ref($s) . '::Iterator'; $s->{"_iterator_$type"} = $class->new(@files); $s->{_last_iterator_file} = $s->{"_iterator_$type"}->selected_file +; return $s->{"_iterator_$type"}->selected_file; } } sub selected_file { my $s = shift; return $s->{_last_iterator_file}; }
Now I can do nested loops:
Thanks so much for the pointers and getting me to think hard about this. Much appreciated!while ($fp->get_next_file) { print $fp->selected_file . "\n"; while ($fp->get_next_nonparseable_file) { print $fp->selected_file . "\n"; } }
$PM = "Perl Monk's";
$MCF = "Most Clueless Friar Abbot Bishop Pontiff Deacon Curate Priest Vicar";
$nysus = $PM . ' ' . $MCF;
Click here if you love Perl Monks
|
|---|