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

G

gargoyle

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.
 
B

brian d foy

gargoyle said:
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?
 
G

gargoyle

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!
 
M

Michele Dondi

#!/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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,994
Messages
2,570,223
Members
46,810
Latest member
Kassie0918

Latest Threads

Top