koaps has asked for the wisdom of the Perl Monks concerning the following question:

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

Replies are listed 'Best First'.
Re: File::System recursion
by mpeever (Friar) on Oct 29, 2008 at 20:50 UTC
    Is there any particular reason you're not using File::Find to do the tree walking?
      I figured if all directories found where returned as objects by File::System::Object, then I can easily create several files within the different objects, verifying content(with md5 maybe) and doing several other operations the File::System::Object has methods for, move, copy, delete, etc.

        OK, I think I understand a little better what you were trying to accomplish.

        This seems to correctly return all the files in the File::System in a hash of hashes:

        #!/usr/bin/perl use strict; use Data::Dumper; use File::Find; use File::System; my $root = File::System->new( "Real", root => '/etc' ); my %files = map { my $file = $_; my %attrs = map { $_ => $file->get_property($_); } ( $_->propertie +s() ); $file => \%attrs; } $root->find( sub { 1 }, () ); print Dumper \%files;
        I realise that doesn't completely do what you want, but it seems to at least get the complete file tree back, without empty hashes...

        To be perfectly honest, I generally use File::Find out of a sense of guilt: I really prefer the interface from File::System::Object $obj -> find()