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
From:
Mark Jason Dominus
Date:
May 20, 2004 06:09
Subject:
Re: FW: File::Find modified to pass arguments to the wanted function (with attachment)
Message ID:
20040520131105.2489.qmail@plover.com
Gisle Aas <gisle@ActiveState.com>:
> Mark Jason Dominus <mjd@plover.com> writes:
> 
> > hv@crypt.org:
> > > Gisle Aas <gisle@ActiveState.com> 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
@dangles.

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


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About