Hi Monks,
I've been playing with File::System for some scripts I need to create and trying to get hash of hashes made as a representation of the directory/file structure I'm using.
The basic layout is:
$root is an object set to some test directory, $path=/root/Code/Test in this case via the File::System->new('Real', root=> $path);
I would like the script to go through the various directories and files it can find and stick them into hashes.
I run into some issues at first because the objects returned by File::System contain extra data that will throw errors if I try to use methods like is_container or has_children against them. I finally got around it by using ->lookup to get a File::System object for the key I want test, and using a technique used in the intermediate perl book for recursive searches on file systems, I was able to get a hash of hashes that looks almost right, but I think I'm doing something wrong because where I expect to see undef's I get => {} which doesn't seem right to me.
The subroutine I use is as follows:
sub paths
{
my $paths_ref = shift;
while ( my ($k,$v) = each %$paths_ref) {
my $l=$root->lookup($k);
if ($l->is_container) {
if ($l->has_children) {
foreach my $c($l->children) {
my $c_ref = {};
$paths_ref->{$c} = $c_ref;
paths($paths_ref->{$c});
}
} else {
$paths_ref->{$k} = undef;
}
} else {
$paths_ref->{$k} = undef;
}
}
}
My data dump looks like this:
$VAR1 = {
'/' => '/',
'/S1_A1/tmp/test' => {},
'/S1_A2/tmp' => {},
'/S1_A1' => {},
'/S1_A1/tmp/test1' => {},
'/test' => undef,
'/S1_A1/tmp/test2' => {},
'/S1_A3' => {},
'/S1_A1/tmp' => {},
'/S1_A2/test' => {},
'/S2_A2' => undef,
'/S1_A3/tmp' => {},
'/S1_A2' => {},
'/S1_A3/tmp/test' => undef,
'/S2_A1' => undef
};
test, test1 and test2 are files
S*_A* and tmp are directories
I just want to make sure things look ok since I'm still newbish with some of this stuff.
Thanks.
-C