in reply to bug of File::Find ?

The bug is in your program:

my ($saved_time, $saved_name) = (-1, '');

You are only remembering one file (with its name and time), and you always keep the latest. You need to reset that variable to (-1, '') (or undef) every time you want to start a fresh search.

Replies are listed 'Best First'.
Re^2: bug of File::Find ?
by iwanthome (Beadle) on Sep 09, 2005 at 07:17 UTC
    thanks! My true code is like this:
    _latest("dire1"); ...do something... _latest("dire2"); ...do something... _latest("dire3"); ...do something... _latest("dire4"); ...do something... sub _latest{ my $file = shift; my @dir; my $saved_time = -1; my $saved_name = ''; sub wanted { if(-f $File::Find::name) { if ((stat($File::Find::name))[9] > $saved_time) { $saved_time = (stat($File::Find::name))[9]; $saved_name = $File::Find::name; } } } @dir = ($file); find(\&wanted, @dir); return $saved_name; }

    I don't know how to reset the variable to (-1, '') in this mode. In my opinion,every time I call _latest function,it reset the variable. But test result is not like that. It don't reset. Would you please help me ? thanks!

      Did you turn on warnings? Don't you get a "Variable ... will not stay shared" warning for you code? If you do, read perldiag about that message. Your error is still that your variables are not reset, except in a more complicated manner.