Ladies and/or gents, I have found a previous solution that gets really close to what I need, but I guess I am just misunderstanding how it actually works. What I want is to be able to convert a directory structure to a hash or arrays such that it will be easily converted to JSON for presentation to a web application. Here.
UPDATE: I didn't want to delete the original post...so, here is where I am:
Here is the current code, which is not doing what I want it to, and am having trouble wrapping my head around how to get to the structure I want to get. First, the data structure I want:
my @dk = ( {data => [
{
text => 'data',
data => {
spriteCssClass => 'folder',
'fullPath' => '/data'
},
contents => [
{
text => 'raw',
data => {
spriteCssClass => 'folder',
fullPath => '/data/raw'
},
contents => [
{
text => 'file1.csv',
data => {
spriteCssClass => 'csv
+',
fullPath => '/data/raw
+/file1.csv',
size => '12345'
}
},
{
text => 'file2.csv',
data => {
spriteCssClass => 'csv
+',
fullPath => '/data/raw
+/file2.csv',
size => '123'
}
}
]
},
{
text => 'rdf',
data => {
spriteCssClass => 'folder',
fullPath => '/data/rdf'
},
contents => [
{
text => 'file1.rdf',
data => {
spriteCssClass => 'rdf
+',
fullPath => '/data/raw
+/file1.rdf',
size => '123456'
}
},
{
text => 'file2.rdf',
data => {
spriteCssClass => 'rdf
+',
fullPath => '/data/raw
+/file2.rdf',
size => '1235'
}
}
]
}
],
}
]
});
Now, the code...which is as far as I have gotten, modifying the previously mentioned code and reference Here.
sub _directoryTree {
my ($directory,$filter) = @_;
my (@files,@dirs);
find(sub{
push @files, $File::Find::name;
},$directory);
foreach my $file(@files) {
chomp $file;
my $ref = \@dirs;
my $truncated = $file;
$truncated =~ s/^$filter//;
foreach my $dir ( split /\//,$truncated ) {
my $i = 0;
next if $dir eq '';
$i++ while ( $ref->[$i] and $ref->[$i]{name} ne $dir );
my $r;
if (-f $file) {
my ($ext,$spriteclass);
if ($file =~ /\.(\w+)$/) {
$ext = $1;
if ($ext eq 'html') {
$spriteclass = 'html';
}
elsif($ext =~ m/png|jpg|jpeg|gif|ico/) {
$spriteclass = 'image';
}
elsif($ext eq 'pdf') {
$spriteclass = 'pdf';
}
else {
$spriteclass = 'html';
}
}
$r= $ref->[$i] ||= {'text' => $dir,
'data' => {
'spriteCssClass' => $spritecla
+ss,
'fullPath' => $file,
'id' => $dir,
'size' => (-s $file),
'type' => $ext
}
} unless $dir =~ m/^\./; #, 'access
+ed' => ctime(stat($file)->atime)
}
else {
$r= $ref->[$i] ||= {'text' => $dir,
'files' => [],
'data' => {
'spriteCssClass' => 'folder',
'fullPath' => $file,
'id' => $dir,
'size' => 0,
'type' => 'folder'
}
};
}
$ref = $r->{'files'};
}
}
return \@dirs;
}