Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

HOP Iterator Ex4.3-- Illegal declaration

by ady (Deacon)
on Mar 03, 2006 at 15:39 UTC ( [id://534251]=perlquestion: print w/replies, xml ) Need Help??

ady has asked for the wisdom of the Perl Monks concerning the following question:

Fellow Monks, --
I'm playing with a directory parsing based on example 4.3 from HOP v.1:
### 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:
C:\tmp>Iterate
Illegal declaration of anonymous subroutine at
C:\tmp\Iterate.pl line 26.


Any ideas why the Iterator {...} construct is illegal in this context ??

Best regards,
Allan Dystrup

Replies are listed 'Best First'.
Re: HOP Iterator Ex4.3-- Illegal declaration
by Fletch (Bishop) on Mar 03, 2006 at 15:42 UTC

    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 { ... }.

      ooOOOOOPPPss, Yes of course -- some months since i've been in Perl land...
      sorry- but Thanks!
      Allan
        Ok, up n' running :
        use strict; use warnings; ### -------------------------- Util ----------------------- sub Iterator (&) { return $_[0] } sub NEXTVAL { $_[0]->() } ### -------------------------- 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; }; } ### -------------------------- FileMask ------------------- 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"; }

        Best regards, Allan Dystrup

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://534251]
Approved by marto
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (3)
As of 2024-04-24 15:46 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found