And its used as such:package My::Package; my $datafile = '/path/to/data.txt'; # each line in file has a numbered key ($key) followed by # some data. file is sorted based on incrementing keys sub itter_maker { # return a function that passes back # data only from lines with keys between # $min and $max my $min = shift; my $max = shift; my $done; my $fh = new IO::File "$datafile"; return( sub { return if $done; my (@data,$row,$key); while (1) { chomp($row = <$fh>) or last; #### # [SNIPPED] munge $row, set $key and @data #### next if $key < $min; $done = 1 && return if ($key > $max); last; } return ($key,\@data); }); }
#!/usr/bin/perl use My::Package; # only loop through lines with "30 <= $key <= 70" my $nextrow = itter_maker(30,70); while (my ($num,$dataref) = $nextrow->()) { ### do stuff }
In reply to Closures as Itterators by blakem
For: | Use: | ||
& | & | ||
< | < | ||
> | > | ||
[ | [ | ||
] | ] |