[VPM] The power of functional programming

Peter Scott Peter at PSDT.com
Tue Jan 9 18:44:17 PST 2007


Inspired by Higher Order Perl, I just created this:

my $iterator = make_iterator(qw(/tmp/in1 /tmp/in2));

my $x;
print $x while $x = $iterator->();

sub make_iterator
{
   my $fh;
   my @files = @_;

   return sub {
     BLOCK : {
       unless ($fh)
       {
	@files or return;
	open $fh, '<', shift @files or die $!;
       }
       while (<$fh>) { return $_ }
       undef $fh;
       redo BLOCK;
     }
   };
}
__END__

See if you can tell:

(1) What does it do?
(2) How does it work?
(3) What is it good for?
-- 
Peter Scott
Pacific Systems Design Technologies
http://www.perldebugged.com/
http://www.perlmedic.com/



More information about the Victoria-pm mailing list