develooper Front page | perl.beginners | Postings from October 2009

Re: About internal working of File::Find

Thread Previous | Thread Next
From:
Jim Gibson
Date:
October 28, 2009 14:15
Subject:
Re: About internal working of File::Find
Message ID:
C70E0281.83D1%JimSGibson@gmail.com
On 10/28/09 Wed  Oct 28, 2009  12:55 PM, "Harry Putnam" <reader@newsguy.com>
scribbled:

> Looking over the perldoc File::Find output (perl 5.8.4)
> 
> I see at finddepth it says:
>   [...] in part
> 
>        "finddepth()" works just like "find()" except that is invokes the
>        &wanted function for a directory after invoking it for the direc-
>        tory's contents.  It does a postorder traversal instead of a pre-
>        order traversal, working from the bottom of the directory tree up
>        where "find()" works from the top of the tree down.
> 
> I'm not so much interested in finddepth() but the part at the end:
>  
>   "find()" works from the top of the tree down.
> 
> Does that mean it will return newer files first if the tree is one
> directory only.

No.
 
> Anyone know if find() works from older to newer, or if there is anyway
> to alter that.  Or is that what `finddepth' does?.. sounds like it
> does what I see find() doing.
> 
> But the description is only in terms of trees.  I'm interested in how
> the actual leaves (files) get processed.

You should not depend upon the order that find or finddepth returns the
files in a directory. The order is not specified. My guess is that they
return the order of the files in the directory, in which older files appear
before newer ones because they were added to the directory first. However,
resorting, compaction, deletions, and additions may disturb that order. In
any case, since File::Find says nothing about the order of the files in a
directory, you should not expect anything in particular.

If you want to process files in a certain order, then save the files in an
array. When find is finished, sort that array and process the files in any
order. For example, if you want to process files in chronological order, in
the wanted routine save the file path and age as an array entry (untested):

    my @files;
    my $dir = '/some/directory/path';

    find( sub{ 
        my $age = -M $File::Find::name;
        push( @files, [ $File::Find::name, $age ] );
        }, $dir);

    for my $entry ( sort { $a->[1] <=> $b[1] } @files ) {
        my($file,$age) = @$entry;
        # process file
    }

(reverse $a and $b if you want the reverse order).




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