Blob


1 #!/usr/bin/perl
3 # Modify the recursive directory dumping routine so it shows the nested
4 # directories through indentation. An empty directory should show up as:
5 #
6 # sandbar, an empty directory
7 #
8 # while a nonempty directory should appear with nested contents, indented two
9 # spaces:
10 #
11 # uss_minnow, with contents:
12 # anchor
13 # broken_radio
14 # galley, with contents:
15 # captain_crunch_cereal
16 # gallon_of_milk
17 # tuna_fish_sandiwch
18 # life_preservers
20 use v5.24;
21 use warnings;
22 use strict;
23 use utf8;
24 use File::Basename;
26 sub data_for_path {
27 my $path = shift;
28 if (-f $path or -l $path) {
29 return undef;
30 } elsif (-d $path) {
31 my %directory;
32 opendir PATH, $path or die "Cannot opendir $path: $!";
33 my @names = readdir PATH;
34 closedir PATH;
35 for my $name (@names) {
36 next if $name eq '.' or $name eq '..';
37 $directory{$name} = data_for_path("$path/$name");
38 }
39 return \%directory;
40 } else {
41 warn "$path is neither a file nor a directory\n";
42 return undef;
43 }
44 }
46 sub dump_data_for_path {
47 my ($path, $data, $level) = @_;
48 if (not defined $data) {
49 print " "x$level;
50 print basename($path)."\n";
51 return;
52 }
53 print " "x$level;
54 print basename($path).":\n";
55 foreach (sort keys %$data) {
56 dump_data_for_path("$path/$_", $data->{$_}, $level+1);
57 }
58 }
59 chdir "/home/jrmu/documents";
60 dump_data_for_path(".", data_for_path("."), 0);