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

Hi Monks!
Anyone would know if its possible here in this code to display the sub directories only after been clinked on?
I am stuck, I can't figure it out even how to check if the link been displayed is a directory or a file, but my goal here is to display all the directories and if clicked, collapse with the files inside. Would be nice to test if directory path have 5 levels deep

#!/perl/bin/perl -w use strict; use CGI qw(:header); use CGI::Carp qw(fatalsToBrowser); use CGI qw/:standard/; my $show = param("show"); my $input_dir = "/cgi-bin/cars"; print header(); print "<html> <head><title>Tree</title> </head>"; MAIN: { my @tree; dirwalk ($input_dir,\@tree); printtree(\@tree); print "</OL>\n"; } sub dirwalk { my ($dir,$tree) = @_ ; push @{$tree},($dir =~ m#([^/]+$)#); opendir DIR, $dir || die "Couldnt open $dir - $!\n"; my @entries = grep !/^\.{1,2}$/, readdir(DIR); closedir (DIR); foreach my $item (@entries) { my $fullpath = "$dir/$item"; if ( -d $fullpath ) { dirwalk ( $fullpath,\@{$tree->[@{$tree}]}); } else { push @{$tree},$item; } } } sub printtree { my $tree = shift; my $c=-1; print "<LI><a href=\"tree.pl?show=true\"><img src=dir.gif border=0>& +nbsp;",shift @{$tree},"</a>/53<br>\n"; print "qqq<OL>\n"; foreach my $item ( @{$tree} ) { #print "<b>$item</b>"; $c++; if (ref $item eq "ARRAY" ) { printtree($item); } else { print "<LI><input type=checkbox> \n"; print $item,"^$c-64\n"; } } print "</OL>"; }

Thanks!!!!

Replies are listed 'Best First'.
Re: Displaying Sub Directories
by gellyfish (Monsignor) on Jul 12, 2006 at 20:40 UTC
Re: Displaying Sub Directories
by state-o-dis-array (Hermit) on Jul 12, 2006 at 20:25 UTC
    If you are trying to display a dynamic directory listing which the user can expand/collapse, why not use javascript?