#!/usr/bin/perl use strict; use warnings; use diagnostics; use Cwd; use File::Find; use File::Basename; use HTML::Entities; my $filename = basename($0); my $rootdir = getcwd; my %tree; $tree{$rootdir} = {}; find( { wanted => \&wanted }, $rootdir ); print "Testing\n"; print_tree( \%tree, 0 ); print "\n"; sub wanted { local $_ = $File::Find::name; if ( -f ) { # only work on data files (skip directories) s{\Q$rootdir\E/}{}; # remove the rootdir string from the path name load_tree( $tree{$rootdir}, fileparse( $_ )); } } # recursively load the hash structure # (first call gets top-level hashref, and file name, path from File::Basename::fileparse) sub load_tree { my ( $href, $name, $path ) = @_; my @dirs = split /\//, $path; push @dirs, '.' if ( $dirs[$#dirs] ne '.' ); my $key = shift @dirs; while ( @dirs and $key ne '.' and exists( $$href{"$key/"} )) { $href = $$href{"$key/"}; $key = shift @dirs; } if ( $key ne '.' and ! exists( $$href{"$key/"} )) { $$href{"$key/"} = {}; load_tree( $$href{"$key/"}, $name, join( '/', @dirs, '' )); } elsif ( $key eq '.' ) { push @{$$href{"$key/"}}, $name; } } # recursively print embedded lists sub print_tree { my ( $href, $indent ) = @_; printf( "%s\n", ' ' x $indent ); }