Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

How to use prune in File::Find

73 views
Skip to first unread message

r_ra...@my-dejanews.com

unread,
Dec 9, 1998, 3:00:00 AM12/9/98
to
I am having trouble understanding the subtlety of using
prune with File::Find. I've read what I could find in
PerlFAQ, perldoc, the camel and llama books, and
http://www.stonehenge.com/merlyn/UnixReview/col16.html


File::File looks like a useful tool but I don't
understand how to use it enough to turn on File::Find::prune.

This is the last iteration of my script...


#!/usr/local/bin/perl -w

use File::Find;

sub wanted {
-T &&
-s > 5120 &&
push(@files, $File::Find::name) &&
($File::Find::prune = 1);
}

find (\&wanted, '.');

foreach (sort @files) { print "$_\n"; }

exit;


It runs but it also lists files passing the tests that
are in the subdirectories. I would appreciate anyone to try
to explain what I am doing wrong or point me to more
information. (BTW, I'm running this on a unix box.)

TIA, rr

-----------== Posted via Deja News, The Discussion Network ==----------
http://www.dejanews.com/ Search, Read, Discuss, or Start Your Own

Robert Lynch

unread,
Dec 10, 1998, 3:00:00 AM12/10/98
to

Pretty crudy whacking away at your script on my part but (note for
testing I dropped the size 5120 -> 512 because I have only small files
in this directory):
--------
use File::Find;

my @files;
sub wanted {
(-T $File::Find::name) &&
(-s $File::Find::name > 512) &&
(push @files, $File::Find::name) &&


($File::Find::prune = 1);
}
find (\&wanted, '.');

foreach (sort @files) { print "$_\n"; }

exit;
---------
[user@ravel perl]$ perl -w finder.pl
./serv.pl
./sock_err.pl
----
[user@ravel perl]$ l serv.pl sock_err.pl
-rwxrw-r-- 1 user user 617 Dec 7 19:57 serv.pl
-rw-rw-r-- 1 user user 2749 Dec 7 21:38 sock_err.pl
----
[user@ravel perl]$ ls -F
2hash.pl client.pl* equiv.pl hh.pl randi.pl
sock_err.pl
CookBookA/ cmc.dll* finder.pl name.pl ref.pl ver.pl
CookBookB/ del.pl foobie pls.pl serv.pl*
----
Should be a better way to write it, but I'm rushing off to dinner...
(Good excuse, no? ;-)

Bob L.
--
Robert Lynch-Berkeley CA USA-r...@best.com
http://www.best.com/~rmlynch/

r_ra...@my-dejanews.com

unread,
Dec 29, 1998, 3:00:00 AM12/29/98
to
In article <36708CE4...@best.com>,

Robert Lynch <rml...@best.com> wrote:
> Pretty crudy whacking away at your script on my part but (note for
> testing I dropped the size 5120 -> 512 because I have only small files
> in this directory):
> --------
> use File::Find;
>
> my @files;
> sub wanted {
> (-T $File::Find::name) &&
> (-s $File::Find::name > 512) &&
> (push @files, $File::Find::name) &&
> ($File::Find::prune = 1);
> }
> find (\&wanted, '.');
>
> foreach (sort @files) { print "$_\n"; }
>
> exit;
> ---------
[snipped run example]

> ----
> Should be a better way to write it, but I'm rushing off to dinner...
> (Good excuse, no? ;-)
>
> Bob L.
> --
> Robert Lynch-Berkeley CA USA-r...@best.com
> http://www.best.com/~rmlynch/

I meant to reply sooner but it's been a busy holiday! Thanks for a
working example but I'm more confused than ever. I decided to use your
script to build a recursive/non-recursive version. The first thing I did
was to remove the prune statement as a test...

#==========
use File::Find;

my @files;
sub wanted {
(-T $File::Find::name) &&
(-s $File::Find::name > 512) &&

(push @files, $File::Find::name);


}
find (\&wanted, '.');

foreach (sort @files) { print "$_\n"; }

exit;
#=========

I expected this to list files in the sub-directories but it
didn't. I'll keep playing with it and maybe the light bulb will
come on eventually.

Thanks again,
Robert R.

0 new messages