#!/usr/bin/perl -w use strict; use warnings; use File::Find; use Data::Dumper qw/ DumperX /; use vars qw/ $TR $TREE $ROOT %DEMDIRS/; die "YOU CANNOT use/require ".__FILE__ if caller(); $ROOT = shift @ARGV; $TREE = $TR = ["$ROOT"]; &find( { wanted => \&build_tree, preprocess => \&Pre, postprocess => \&Post, bydepth => 0, # don't override ,} , $ROOT, ,); print DumperX($TREE) if @ARGV > 1; print "\n"; DumpTree($TREE); exit; sub Post { $DEMDIRS{$File::Find::dir} = $TR; # warn "post $File::Find::dir"; } sub Pre { my @it = sort { -d $b <=> -d $a } @_; return @it; } sub build_tree { if( $_ eq '.' or $_ eq '..') { return; } if(-d) { my $tTR = [$_]; if(exists $DEMDIRS{$File::Find::dir}) { $TR = $DEMDIRS{$File::Find::dir}; } $DEMDIRS{$File::Find::dir} = $TR; push @$TR, $tTR; $TR = $tTR; return; } push @$TR, $_; } sub DumpTree { my $ARRAY = shift or die "hello?"; my $DEPTH = shift || 0; # only 0 the first time my $atWhat = shift || 0; print shift(@$ARRAY), "\n" unless $DEPTH; my $FILE_OR_REF; while(defined($FILE_OR_REF = shift(@$ARRAY))) { if(ref $FILE_OR_REF) { if($DEPTH) { print Depthy($DEPTH,$atWhat); print "\n"; print Depthy($DEPTH, $atWhat); } else { print "\n"; } print '+'; print '---'; print shift(@$FILE_OR_REF); print "\n"; DumpTree($FILE_OR_REF, $DEPTH + 1, $atWhat + 1); next; } else { if($DEPTH) { print Depthy($DEPTH, $atWhat); } else { print '| '; } print $FILE_OR_REF; print "\n"; } } } sub Depthy { my $depth = shift; my $opy = shift || 0; my $string; for my $i (1..$depth) { if($opy) { $string .= '|'; $string .= ' ' x 3; } else { $string .= ' ' x 4; } } return $string; } __END__