Ron Goral wrote:
Gunnar Hjalmarsson wrote:
One possible solution is to move the ProcessFile() function out
from FindPath(), so the former is no longer a nested sub:

    sub ProcessFile {
        my ($a_files, $file_name) = @_;
        push @$a_files, $File::Find::name if $_ eq $file_name;
    }

and call ProcessFile() from FindPath() with arguments:

    find( \&ProcessFile( [EMAIL PROTECTED], $file_name ), $file_path );

And last but not least:

    use warnings;

;-)

Actually, I am using warnings. However, in the 'real' code, I have placed the the call to 'find' within an eval block so that I can manage the errors. Is this why I did not receive any warnings?

I get the same warnings also with such an eval block.

The difficulties with moving ProcessFile out of FindPath is that
any return values ProcessFile might have are ignored and it can
take no arguments (this is from http://search.cpan.org/~nwclark/perl-5.8.5/lib/File/Find.pm#The_wanted_function).

No return values does not matter, since my suggestion didn't make use of return values from ProcessFile, but the rest does. Jenda showed us a way to modify that approach to working code.

Sorry for posting non-tested code. :(

<snip>

There is a light, however.  If ProcessFile is actually an anonymous
sub and called this way:

my $processfile = sub {if ($_ eq $file_name){push (@a_files, $File::Find::name);}};

find(\&$processfile, $file_path);

There are no errors and @a_files is populated (and depopulated) as
it should be.

Yes, that seems to be a nice solution. You can even pass it to find() by just saying:

    find( $ProcessFile, $file_path );

If you haven't already, to get an understanding of what the original
problem actually was about, you can read the "Variable '%s' will not
stay shared" section in "perldoc perldiag".

--
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl

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




Reply via email to