Problem is, because all of the variables are global, it doensn't unwind...
you have scope issues
here is a quick example to guide you, notice the use of my
#!/usr/bin/perl
use strict;
my $current = '/usr/local';
my $dir_start = '/home';
print "Current is '$current', starting with '$dir_start'\n\n";
recurse_directory( $dir_start );
print "\nDone.\n\n";
sub recurse_directory {
## variables
my $current;
my @local_list;
## get the dir we are in
$current = $_[0];
## get contents
## no "." or ".." dirs
opendir( CURRENTDIR, "$current" )
or die "Can not open $current: $!";
@local_list = grep !/^\.\.?$/, readdir CURRENTDIR;
closedir( CURRENTDIR )
or die "Can not close $current: $!";
## now loop over the contents
foreach my $x ( @local_list ) {
my $file = $current . '/' . $x;
recurse_directory( $file ) if ( $file -d );
print "$file\n";
}
}
there is quite a bit wrong with this snippet, as i am sure will be pointed out, but this should help you with your scope problems.
the in depth differences between global, local, and my variables may be more than you are looking for, but a good place to start is page 189 of the Camel book, 2nd edition( no idea on the page in 3rd edition, sorry )
Will perl for money
JJ Knitis
(901) 756-7693
gt8073a@industrialmusic.com |