Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

Re: File::Find::prune problems

by amarquis (Curate)
on Mar 28, 2008 at 12:52 UTC ( [id://676966]=note: print w/replies, xml ) Need Help??


in reply to File::Find::prune problems

I tested with:

find(\&wanted, "/usr/home/amarquis"); sub wanted { print $File::Find::name . "\n"; if ($File::Find::dir =~ /images/) { $File::Find::prune = 1; } }

And indeed it continued to call wanted for files in "images" but did not go into subdirectories. But since you already have in place a check to see if you want to prune a directory, why not extend that check to before you process a file? For example:

sub wanted { # Prune this directory so we don't recurse farther, # And return so we don't process this file. if ($File::Find::dir =~ /images/) { $File::Find::prune = 1; return; } # Now we can do our processing: print $File::Find::name . "\n"; }

Replies are listed 'Best First'.
Re^2: File::Find::prune problems
by runrig (Abbot) on Mar 28, 2008 at 16:38 UTC
    You are pruning too late. You want to prune when $File::Find::name or $_ (not $File::Find::dir) matches the directory:
    if ( -d and /images/ ) { $File::Find::prune = 1; return; } # Do your processing

      Thank you for the correction. I just tried it out, works great. For some reason I thought from the documentation that if you set prune to 1 while it was simply looking at a directory it would prune the parent directory.

      This is a really important point - prune on $File::Find::name match works best. e.g.

      my $crnt_file = $File::Find::name; my $root = $::root_dir; if (scalar grep($crnt_file =~ m/^$root$_/, @::prune) != 0) { print Dumper($crnt_file); $File::Find::prune = 1; return; }

      This tip helped heaps. Thanks.

Re^2: File::Find::prune problems
by spx2 (Deacon) on Mar 28, 2008 at 13:15 UTC
    thank you very much amarquis,it was a very elegant way to fix the problem.
    I've tried to get into cpanforum at file::find module but they I search for it and can't find it.
    I believe this is a BUG of File::Find and I would like to notify it.
    How could I do this ?

      I'm not sure based on the wording in the documentation that it is a bug. I don't think it is a great feature but, hey, I'm not the author. You should see a link to the bug tracker for a module on its page at CPAN.org, though. (Edit: Ignore what I said here, and listen to anonymonk above)

      clinton's File::Find::Rule looks to be a pretty sweet interface, though, that fixes your issue with prune neatly. I'd check it out.

      CPAN Forum is for discussion, not for bugs. RT is for bugs.

      Since File::Find belongs to the Perl core distro, and it is not a dual-life module (i.e. it does not have another version seperately published on CPAN like for instance threads::shared), you can find its discussion forum at /dist/perl. Similarly, the bug queue is at bugs.perl.org and you report with perlbug.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://676966]
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 08:43 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found