develooper Front page | perl.perl5.porters | Postings from May 2004

Re: FW: File::Find modified to pass arguments to the wanted function (with attachment)

Thread Previous | Thread Next
Mark Jason Dominus
May 20, 2004 06:09
Re: FW: File::Find modified to pass arguments to the wanted function (with attachment)
Message ID:
Gisle Aas <>:
> Mark Jason Dominus <> writes:
> >
> > > Gisle Aas <> wrote:
> > > :Another option is simply to pass a closure that capture the arguments
> > > :you want your callback to receive.  
> > > :But again, I think File::Find is fine as it is.
> > > 
> > > I agree, 
> > 
> > 
> > It's not always convenient to do that.
> The sub{} wrapper will seldom be much more typing than providing args
> any another way, so I don't find it very inconvenient either.

I didn't say anything about the quantity of typing, and I don't think
the amount of typing saved or not saved is important.  

I said that it isn't always convenient to define the callback function
in the same scope as the call to File::Find::find.

For example, it is not convenient to have a module which provides
callback functions to be used with File::Find.

Suppose for example one is trying to locate all the dangling symbolic
links under a directory.  One might write

        use File::Find;
        { my @dangles;
          my $dangling_symlinks = 
                sub { push @dangles, $File::Find::name if -l && ! -e };
          find({wanted => $dangling_symlinks}, $TOP);

          # Now do something with @dangles

and this is no problem.  But suppose the 'dangling_symlinks' function
was provided by a module, so one would write:

        use File::Find;
        use File::Find::Common 'dangling_symlinks';

        my @dangles;
        find({wanted => \&dangling_symlinks}, $TOP);
        # Now do something with @dangles

No, this doesn't work, and it can't be made to work, because
File::Find::Common::dangling_symlinks is not in the scope of my

Normally one would simply pass an argument to
File::Find::Common::dangling_symlinks with a reference to the target
array.  But the File::Find API does not support this.  The proposal is
simply to support it.

Thread Previous | Thread Next Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at | Group listing | About