File::Find is a very useful module, but it isn't
particulary easy to use. Here is a snippet which
does what File::Find does without the module. Modify to
your hearts desire.
Note: since this is so completely against the
spirit of CPAN etc, I'm expecting your flames and I've put
my asbestos underwear on ready ;-)
Note2: I wrote this snippet originally to delete
an nearly infinite directory of files which a user created
on the server. 'rm -rf' wouldn't and I didn't think of File::Find until too late ;-)
Note3: In response to merlyn's post below, you can uncomment the
lines marked if you want a symlink safe version. Though recursive symlinks
are usually a bad idea in any case!
#!/usr/bin/perl -w
#
# Demonstration of going it alone without File::Find
use strict;
use Cwd;
die "Syntax: $0 <dir>" unless @ARGV == 1 && -d $ARGV[0];
find(0, $ARGV[0]);
exit;
sub find
{
my ($level, $dir) = @_;
my ($back) = cwd()
or die "Failed to read current working directory";
chdir($dir) or die "Couldn't cd to $dir";
opendir(DIR, ".") or die "Failed to opendir()";
my @files = grep { $_ ne '.' && $_ ne '..' } readdir(DIR);
closedir(DIR) or die "Failed to closedir()";
for my $file (@files)
{
# If you want a symlink safe version write this
# lstat($file);
# if (-d _)
if (-d $file)
{
# Do something with a directory
print ' ' x $level, "Dir: '$file'\n";
# ...then recurse if you want to
find($level+1, $file);
}
else
{
# Do something with a file (don't recurse)
print ' ' x $level, "File: '$file'\n";
}
}
chdir $back
or die "Failed to chdir $back";
}