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

File::Find appears to be very flexible in how it parses directory trees, but it takes a coderef to a subroutine.

Is it possible to use a method? I have some state information which would be really useful to access from within the subroutine, and passing the self reference to the function would be extremely helpful. I guess I could assign self to a global module variable and access this from within the subroutine, but this seems like a kludge.

Any suggestions would be greatly appreciated.

Thanks!

Replies are listed 'Best First'.
Re: Can File::Find call a method?
by afoken (Chancellor) on Mar 14, 2012 at 20:47 UTC

    File::Find can't, but the wanted function can:

    use File::Find qw( find ); my $obj=Some::Class->new(...); find(sub { $obj->someEntry($_) },...);

    Inside a method:

    use File::Find qw( find ); sub myMethod { my $self=shift; find(sub { $self->otherMethod($_) },...); }

    Alexander

    --
    Today I will gladly share my knowledge and experience, for there are no sweeter words than "I told you so". ;-)