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

Re: About internal working of File::Find

Thread Previous | Thread Next
From:
Harry Putnam
Date:
October 28, 2009 15:18
Subject:
Re: About internal working of File::Find
Message ID:
87d447i2mn.fsf@newsguy.com
Jim Gibson <jimsgibson@gmail.com> writes:

> 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).

I see the final result but am slightly mystified by some of the
notation.  First off the formulation of the find() function.

I've seen it before but don't really understand it.

Seems like the guts of find() are missing, but I know they are not.

I guess it could be expressed:

  find( sub{..results of this sub routine},$dir);

Like you might do with `find($var1,$var2);'
But there you would expect to see `find()' have some lines of
processing to the two elems of @_ passed in.

In your formulation it appears sub processes itself, and somehow
`find()' knows to do this to each file in $dir

I'm sure its all good perl but it just looks confusing to me.

-------        ---------       ---=---       ---------      -------- 

What are the brackets ([]) doing here:
  push( @files, [ $File::Find::name, $age ] );

and what is this creature:
   my($file,$age) = @$entry;
                    ^^


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