home | O'Reilly's CD bookshelfs | FreeBSD | Linux | Cisco | Cisco Exam  

Book Home Programming PerlSearch this book

32.21. File::Find

use File::Find;

# Print out all directories below current one.
find sub { print "$File::Find::name\n" if -d }, ".";

# Compute total space used by all files in listed directories.
@dirs = @ARGV ? @ARGV : ('.');
my $sum = 0;
find sub { $sum += -s }, @dirs;
print "@dirs contained $sum bytes\n";

# Alter default behavior to go through symlinks
# and visit subdirectories first.
find { wanted => \&myfunc, follow => 1, bydepth => 1 }, ".";
The File::Find module's find function recursively descends directories. Its first argument should be a reference to a function, and all following arguments should be directories. The function is called on each filename from the listed directories. Within that function, the $_ variable is set to the basename of the current filename visited, and the process's current working directory is by default set to that directory. The package variable $File::Find::name is the full pathname of the visited filename. An alternative calling convention takes as its first argument a reference to a hash containing option specifications, including "wanted", "bydepth", "follow", "follow_fast", "follow_skip", "no_chdir", "untaint", "untaint_pattern", and "untaint_skip", as fully explained in the online documentation. This module is also used by the standard find2perl(1) translator program that comes with Perl.

Library Navigation Links

Copyright © 2001 O'Reilly & Associates. All rights reserved.