### Iterate.pl
use strict;
use warnings;
use Iterator_Utils;
# ---------------------------------------------------------
sub interesting_files {
my $is_interesting = shift;
my @queue = @_;
return Iterator {
while (@queue) {
my $file = shift @queue;
if (-d $file) {
opendir my $dh, $file or next;
my @newfiles = grep {$_ ne "." && $_ ne ".."} readdir $dh;
push @queue, map "$file/$_", @newfiles;
}
return $file if $is_interesting->($file);
}
return;
};
}
# --------------------------------------------------------
sub $is_WrappedXml {
my $file = shift;
return 1 if $file =~ /WrappedXml.{0,5}\.xml/i;
return;
}
# --------------------------------------------------------
# MAIN
my $WrappedXml_file = interesting_files(\$is_WrappedXml, '.');
while (defined($file = NEXTVAL($WrappedXml_file)))
print $file;
}
where
### Iterator_Utils.pm
package Iterator_Utils;
use base Exporter;
@EXPORT_OK = qw(NEXTVAL Iterator);
%EXPORT_TAGS = ('all' => \@EXPORT_OK);
sub NEXTVAL { $_[0]->() }
sub Iterator (&) { return $_[0] }
1;
When running program Iterate.pl i get the error:The problem is probably with this line:
sub $is_WrappedXml {
You either want a named sub (drop the $) or you want my $is_WrappedXml = sub { ... }.
use strict;
use warnings;
use Iterator_Utils qw(NEXTVAL Iterator);
# -----------------------------------------------------------------------------
sub interesting_files {
my $is_interesting = shift;
my @queue = @_;
return Iterator {
while (@queue) {
my $file = shift @queue;
if (-d $file) {
opendir my $dh, $file or next;
my @newfiles = grep {$_ ne "." && $_ ne ".."} readdir $dh;
push @queue, map "$file/$_", @newfiles;
}
return $file if $is_interesting->($file);
}
return;
};
}
# -----------------------------------------------------------------------------
sub is_WrappedXml {
my $file = shift;
return 1 if $file =~ /WrappedXml.{0,5}\.xml/i;
return;
}
# -----------------------------------------------------------------------------
# MAIN
my $WrappedXml_file = interesting_files(\&is_WrappedXml, '.');
while (defined(my $file = NEXTVAL($WrappedXml_file))) {
print "$file\n";
}
perlmonks.org content © perlmonks.org and ady, Fletch
prlmnks.org © 2006 edmund von der burg (eccles & toad)
v 0.03