August 1999 | Return to the Perl Hardware Store | Slide #32 |
sub make_iterator {
my ($file, $key) = @_; my $fh = do { local *FH }; open $fh, $file or return;
my $iterator = sub { my $line; while ($line = <$fh>) { return $line if index $line, $key >= 0; } return; }; return $iterator; }
Anonymous subroutine is a closure
my variables are captured by the closure
Each call to make_iterator constructs a new closure with new private state variables
Next | Copyright © 1999 M-J. Dominus |