Chris wrote:
I'm writing a script that maps out a file system. Here is what I have
so far:
#!/usr/bin/perl -w
use strict;
#use Data::Dumper;
sub data_for_path {
my $path = shift;
if (-f $path or -l $path) {
return undef;
You normally don't need to return undef, perl will do the right thing and
correctly distinguish between list and scalar context.
You are stat-ing the same file name three times. It would be more efficient
to just stat it once:
my $path = shift;
lstat $path or die "Cannot lstat $path: $!";
if ( -f _ or -l _ ) {
return;
}
if ( -d _ ) {
}
if (-d $path) {
my %directory;
opendir PATH, $path or die "Cannot opendir $path: $!";
my @names = readdir PATH;
closedir PATH;
for my $name (@names) {
next if $name eq '.' or $name eq '..';
$directory{$name} = data_for_path("$path/$name");
}
return \%directory;
}
warn "$path is neither a file nor a directory\n";
return undef;
}
#print Dumper(data_for_path(".."));
my $filesystem_ref = data_for_path("..")
You are missing the semicolon at the end of the statement which is why you are
getting the error messages:
my $filesystem_ref = data_for_path( ".." );
foreach (keys %$filesystem_ref) {
if ( $filesystem_ref->[$_] = undef) {
You are assigning undef to the first element of the array dereferenced from
$filesystem_ref with means that it will always evaluate to false and nothing
will be printed. The correct way to do what you want is:
if ( defined $filesystem_ref->{ $_ } ) {
But why not just filter out those undef values when you populate the hash in
the subroutine?
print $_."\n";
}
}
John
--
Perl isn't a toolbox, but a small machine shop where you
can special-order certain sorts of tools at low cost and
in short order. -- Larry Wall
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/