structure, you could have used redo:while (1) { ... next if () ... last }
And you could generalize it a bit further, by making a general-purpose iterator maker that itself takes a CODE ref to call in the middle:LINE: { chomp($row = <$fh>) or last LINE; redo LINE if $key < $min; $done = 1 && return if ($key > $max); }
sub itter_maker { # return a function that passes back data allowed b +y the given predicate subroutine my $predicate = shift; #CODE ref my $done = 0; my $fh = IO::File->new($datafile) or die "can't open $datafile: $!\n +"; return( sub { return if $done; my (@data,$row,$key); my $found = 0; while( ! $found && ! $done && defined( $row= <$fh> ) ) { chomp($row); ($found, $done) = $predicate->($row, \$key, \@data); } return ($key,\@data) if $found; return; }); }
update: changed to redo, fixed predicate call syntax.
update2: incorporated tye's suggestion, thanks!
In reply to Re: Closures as Itterators
by bikeNomad
in thread Closures as Itterators
by blakem
For: | Use: | ||
& | & | ||
< | < | ||
> | > | ||
[ | [ | ||
] | ] |