On 8/16/07, Dan Sopher <[EMAIL PROTECTED]> wrote:
>
>
> Hello. The following code example creates a list of regular files in a
> directory. Using File::Find, I'm unable to localize an array to hold the
> list of files. Is there a way to create the list with a localized array?
> TIA.
>
>
>
> #!/usr/bin/perl -w
> ## Create a list of regular files in a directory.
>
> use strict;
> use File::Find;
>
> my @list; ## Don't want this here
> if (1) {
>         ## I'd like @list to be localized to this scope,
>         ## and not in main
>
>         find ( { wanted=>\&found, no_chdir=>1 }, "/var/SAMPLES" );
>
>         print @list;
> }
>
> sub found {
>         push @list, $_ if -f $_;
> }

There are a couple ways, but the my favorite is what is called a closure:

#!/usr/bin/perl

use strict;
use warnings;

use File::Find;

if (1) {
    my @list;

    find ( { wanted => sub { push @list, $_ if -f }, no_chdir=>1 },
"/var/SAMPLES" );

    print @list;
}

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/


Reply via email to