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

File::Find::find() is depth-first?

4 views
Skip to first unread message

gargoyle

unread,
Jan 9, 2005, 7:00:33 PM1/9/05
to
The File::Find man page says, under DESCRIPTION:

"find()" does a depth-first search over the given
@directories in the order they are given.

What exactly does depth-first mean here? I'm asking because to me it
signifies that filesystem children objects will come before their
parent. But the opposite happens in Perl 5.8.4, both on OpenBSD 3.6,
and the ActiveState 5.8.4 build 810. Well that's the behavior I get out
of this script anyway:

#!/usr/bin/perl -w

use File::Find;

@dirs = @ARGV;
find({ wanted => \&wanted, follow => 0, no_chdir => 1 }, @dirs);

sub wanted {
print "$_\t$File::Find::name\n";
}

Also, I checked an old Debian(3.0) Linux box sitting around here (with
Perl 5.6.1 installed) and its man page doesn't mention find() as being
depth-first.

brian d foy

unread,
Jan 9, 2005, 9:19:04 PM1/9/05
to
In article <BAjEd.4807$vM4...@bignews3.bellsouth.net>, gargoyle
<garg...@no.spam> wrote:

> The File::Find man page says, under DESCRIPTION:
>
> "find()" does a depth-first search over the given
> @directories in the order they are given.
>
> What exactly does depth-first mean here? I'm asking because to me it
> signifies that filesystem children objects will come before their
> parent.

"Depth first" has the usual meaning:

http://en.wikipedia.org/wiki/Depth-first_search

Are you thinking about finddepth(), which invokes the callback
on the directory after it has already processed the children?

--
brian d foy, com...@panix.com
Subscribe to The Perl Review: http://www.theperlreview.com

gargoyle

unread,
Jan 9, 2005, 11:12:37 PM1/9/05
to
On 2005-01-10, brian d foy <com...@panix.com> wrote:
> http://en.wikipedia.org/wiki/Depth-first_search
>
> Are you thinking about finddepth(), which invokes the callback
> on the directory after it has already processed the children?

Ahh that explains it! It's depth-first as opposed to breadth-first.

I was mainly concerned that find() would return parent nodes first,
because the code I'm working on now depends on that behavior. But
either of those algorithms would work fine.

Thanks for clearing that up!

Michele Dondi

unread,
Jan 10, 2005, 9:34:34 AM1/10/05
to
On Mon, 10 Jan 2005 00:00:33 GMT, gargoyle <garg...@no.spam> wrote:

>#!/usr/bin/perl -w

As side notes: better

use warnings;

nowadays. Also,

use strict;

always!

>use File::Find;
>
>@dirs = @ARGV;
>find({ wanted => \&wanted, follow => 0, no_chdir => 1 }, @dirs);

Why not

find({ wanted => \&wanted, follow => 0, no_chdir => 1 }, @ARGV);

incidentally?


Michele
--
{$_=pack'B8'x25,unpack'A8'x32,$a^=sub{pop^pop}->(map substr
(($a||=join'',map--$|x$_,(unpack'w',unpack'u','G^<R<Y]*YB='
.'KYU;*EVH[.FHF2W+#"\Z*5TI/ER<Z`S(G.DZZ9OX0Z')=~/./g)x2,$_,
256),7,249);s/[^\w,]/ /g;$ \=/^J/?$/:"\r";print,redo}#JAPH,

0 new messages