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

I was always told not to alter the files and directory structure when doing a File::Find. So I do a find pushing the results into a list then after the find is complete I perform my action on the files. Is this silly? The code structure that I use is as follows:

my @file_list; my $file; my $path = "/home/blm/url/"; find (\&wanted, $path); foreach $file (@file_list) { ... } sub wanted{ if (-f) { push @file_list, $_; } }

Replies are listed 'Best First'.
Re: Shouldn't modify file or directory structure when in File::Find?
by Zaxo (Archbishop) on Sep 17, 2002 at 23:25 UTC

    Your technique is fine, not silly at all. A list of files isn't that big a memory hog, and that procedure prevents bad interactions between &wanted and the processing you do.

    For instance, if your processing created backups in a yet unexamined directory, you would find yourself making backups of the backups, perhaps without end.

    After Compline,
    Zaxo

Re: Shouldn't modify file or directory structure when in File::Find?
by PodMaster (Abbot) on Sep 18, 2002 at 02:57 UTC
    You might wanna use "postprocess", as in
    find({ wanted =>\&wanted, postprocess => sub { unlink @file_list; }, }, $path );
    seeing how you're only dealing with files.

    ____________________________________________________
    ** The Third rule of perl club is a statement of fact: pod is sexy.