Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

bug of File::Find ?

by iwanthome (Beadle)
on Sep 09, 2005 at 07:04 UTC ( [id://490444]=perlquestion: print w/replies, xml ) Need Help??

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

I would use File::Find to get the latest file in many directory. After I test the code,I found that code work normal if I send one directory to File::Find. But it would get wrong result if I send more than one directory to module. It seems like that File::Find would remember the variable.

#!/usr/bin/perl use File::Find; $file = "/home/cmbnms/data/tftp/99.1.124.251"; my ($saved_time, $saved_name) = (-1, ''); sub biggest { 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; } } return; } @dir = ($file); find(\&biggest, @dir); print "Biggest file $saved_name in is $saved_time long.\n"; $file = "/home/cmbnms/data/tftp/99.1.124.252"; @dir = ($file); find(\&biggest, @dir); print "Biggest file $saved_name in is $saved_time long.\n"; $file = "/home/cmbnms/data/tftp/10.1.102.237"; @dir = ($file); find(\&biggest, @dir); print "Biggest file $saved_name in is $saved_time long.\n"; $file = "/home/cmbnms/data/tftp/10.1.99.100"; @dir = ($file); find(\&biggest, @dir); print "Biggest file $saved_name in is $saved_time long.\n"; $file = "/home/cmbnms/data/tftp/10.1.99.200"; @dir = ($file); find(\&biggest, @dir); print "Biggest file $saved_name in is $saved_time long.\n";
execute result is like this:
Biggest file /home/cmbnms/data/tftp/99.1.124.251/2005-9-/15-20-24-99.1 +.124.251.conf in is 1126164026 long. Biggest file /home/cmbnms/data/tftp/99.1.124.252/2005-9-8/15-20-26-99. +1.124.252.conf in is 1126164027 long. Biggest file /home/cmbnms/data/tftp/10.1.102.237/2005-9-8/17-21-30-10. +1.102.237.conf in is 1126171290 long. Biggest file /home/cmbnms/data/tftp/10.1.102.237/2005-9-8/17-21-30-10. +1.102.237.conf in is 1126171290 long. Biggest file /home/cmbnms/data/tftp/10.1.102.237/2005-9-8/17-21-30-10. +1.102.237.conf in is 1126171290 long.

you can see that the last 3 result is same, but it is not truth.

thanks for your help!

Considered: marto: Retitle : "Possible File::Find bug" ?
Unconsidered: Chady: enough keep votes (edit:16 keep:23 del:0)

Replies are listed 'Best First'.
Re: bug of File::Find ?
by Corion (Patriarch) on Sep 09, 2005 at 07:06 UTC

    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.

      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.

Re: bug of File::Find ?
by iwanthome (Beadle) on Sep 09, 2005 at 07:32 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!

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://490444]
Approved by marto
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others having a coffee break in the Monastery: (4)
As of 2024-03-29 06:37 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found