I don't need a complete program, just the minimum necessary to illustrate
the basic recursive concepts.
All help is appreciated.
-S
Here's a complete program that doesn't do much except print everything out:
perl -lMFile::Find -e'find(sub{print},$ARGV[0])' <insert directory path here>
I suggest you look at the File::Find code if you want to see the basic
concepts (though it does the advanced things as well... checking for
symlinks, etc).
--
Sam
If your language is flexible and forgiving enough, you can prototype
your belief system without too many core dumps.
--Larry Wall
Of course you will really want to use the module File::Find in real life
but just for shits and giggles - here is something I knocked up when
someone asked about creating a directory tree - this outputs nested HTML
lists showing the directory structure and the files - the reason *I*
didnt use File::Find was that it seemed too difficult to recursively
process the tree structure into an array as seemed natural - of course
you will almost certainly run out of memory in a deep tree with lots
of files and has *ahem* undefined behaviour in the face of symbolic links
and all those other nasty things that File::Find shields you from ...
#!/usr/bin/perl -w
use strict;
my $input_dir = $ARGV[0] || '.' ;
MAIN:
{
my @tree;
dirwalk ($input_dir,\@tree);
print "<OL>\n";
printtree(\@tree);
print "</OL>\n";
}
sub dirwalk
{
my ($dir,$tree) = @_ ;
push @{$tree},($dir =~ m#([^/]+$)#);
opendir DIR, $dir || die "Couldnt open $dir - $!\n";
my @entries = grep !/^\.{1,2}$/, readdir(DIR);
closedir (DIR);
foreach my $item (@entries)
{
my $fullpath = "$dir/$item";
if ( -d $fullpath )
{
dirwalk ( $fullpath,\@{$tree->[@{$tree}]});
}
else
{
push @{$tree},$item;
}
}
}
sub printtree
{
my $tree = shift;
print "<LI>",shift @{$tree},"/\n";
print "<OL>\n";
foreach my $item ( @{$tree} )
{
if (ref $item eq "ARRAY" )
{
printtree($item);
}
else
{
print "<LI>\n";
print $item,"\n";
}
}
print "</OL>";
}
Have fun ...
/J\
--
Jonathan Stowe <j...@gellyfish.com>
<http://www.gellyfish.com>
Hastings: <URL:http://dmoz.org/Regional/UK/England/East_Sussex/Hastings>