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:
Gisle Aas
Date:
May 20, 2004 06:54
Subject:
Re: FW: File::Find modified to pass arguments to the wanted function (with attachment)
Message ID:
lrhdubgq6j.fsf@caliper.activestate.com
Mark Jason Dominus <mjd@plover.com> writes:

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

You said that it was not always practical to have the callback
function in the same scope and from context that using a closure to
bind the variable to the callback was not always convenient.  The only
inconvenience I can see is the amount of typing.

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

I'm just saying that:

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

will work as well as the proposal.

> The proposal is simply to support it.

Yup.

Regards,
Gisle

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