Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

$File::Find::prune

by greenFox (Vicar)
on Mar 09, 2001 at 12:32 UTC ( [id://63196]=perlquestion: print w/replies, xml ) Need Help??

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

I expect I have completely misunderstood the purpose of $File::Find::prune, here is my code-
my $dir = '/some_dir'; find (\&process, $dir); sub process { my $dir = $File::Find::dir; if ($dir =~ /^\/some_dir\/sub_dir/){ print "skipping $dir\n"; $File::Find::prune = 1; return; } print "$dir/$_\n"; }
what I expected to happen is that I would get one "skipping..." message and that find would not descend below sub_dir. This is not what happens, instead I get lots of "skipping..." messages. What is $File::Find::prune supposed to do and how do I do what I want to. Thanks.

Replies are listed 'Best First'.
Re: $File::Find::prune
by btrott (Parson) on Mar 09, 2001 at 13:16 UTC
    This is just an error in your logic, basically. What you want is probably:
    if ($File::Find::name =~ m!^/some_dir/sub_dir!) { print "skipping $dir\n'; $File::Find::prune = 1; return; }
    In other words, test $File::Find::name, not $File::Find::dir. When process gets called, dir holds the name of the *containing* directory. So when process gets called for the entry "/some_dir/sub_dir", dir is "/some_dir". You're expecting it to be "/some_dir/sub_dir", but it's not that until you've actually descended into "sub_dir". So you're skipping all the entries below "sub_dir" but not descending into "sub_dir" itself.

    Make sense?

    By the way, "$dir/$_" is just $File::Find::name. I think.

      $File::Find::name was it :) thanks to yourself and Tyke who tried to point me in the right direction though I still managed to keep looking at $File::Find::dir and scratch my head wondering :) . Cheers.
Re: $File::Find::prune
by arhuman (Vicar) on Mar 09, 2001 at 12:58 UTC
    I may be wrong but $dir is always set to '/some_dir' in process..

    So I can't explain how you get "skipping" printed.


    "Trying to be a SMART lamer" (thanx to Merlyn ;-)
      If you add the line
      print "Got name = [$File::Find::name] dir = [$File::Find::dir]\n";
      before your if statement, I think you'll see what's going wrong. $File::Find::dir doesn't contain what you think it does.
      -- I knock my pate and fancy wit will come Knock as I please, there's no one at home a pontiff paraphrased

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others wandering the Monastery: (6)
As of 2024-03-28 19:14 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found