Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:
#!/usr/bin/perl -w use strict; use CGI qw(:header); use CGI::Carp qw(fatalsToBrowser); use CGI qw/:standard/; my $q = new CGI; my $show = param("show"); #my $input_dir = $ARGV[0] || '.' ; my $input_dir = "../my_directory"; my $arg_name = $q->param( 'arg_name' ) || ''; #arg must be "files" my $file_name = $q->param( 'fname' ) || ''; #arg must be a file name +found in the directory above print header(); print "<html> <head><title>Directory Tree 4</title> <script type=\"text/javascript\" src=\"../../aqtree3.js\"></script> <script type=\"text/javascript\" src=\"../../aqtree3clickable.js\"></s +cript> <link rel=\"stylesheet\" href=\"../../aqtree3.css\"> <link rel=\"stylesheet\" href=\"../../aqtree3clickable.css\"> </head> <body> Starting Directory<br> "; MAIN: { my @tree; dirwalk ($input_dir,\@tree); #print "L32-<OL>\n"; 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 "<ul class=\"aqtree3clickable\"><li><a href=\"#\"> ",shif +t @{$tree},"</a><br>\n"; foreach my $item ( @{$tree} ) { #print "<b>$item</b>"; $c++; if (ref $item eq "ARRAY" ) { printtree($item); } else { print "<ul><li><a href=\"tree.pl?arg_name=files&fname=$item\" +>\n"; print $item,"</a></li></ul>\n"; if($arg_name eq "files") { # I am trying to insert code to ca +ll get_modules to find all the modules used in each Perl script here get_modules($file_name); #print "here"; } } } print "</li>"; print "</ul>"; } sub get_modules { my ($file) = @_ ; my (@all_files, @all_mod); my $dir = "../../my_directory"; #this is the same as in $input_dir ab +ove opendir(DIR, $dir) or die $!; while (my $file = readdir(DIR)) { # just files next unless (-f "$dir/$file"); # Use a regular expression to find files ending in .pl and .pm in t +he directory next unless ($file =~ m/(\.pl|\.pm)$/); print "$file\n"; push @all_files, $file; } closedir(DIR); if( ($arg_name eq "files") && ( $file_name ) ) { print "<br>Perl Modules used by $file_name:<br>\n\n"; foreach my $files(@all_files) { open (FILE, "$dir/$files"); while(my $line= <FILE> ){ if(grep /\buse\b/, $line) { if( ($files eq "$file_name") && ($line!~/^#/g) ) { $line=~s/\buse\b//g; print "$line"; } } } close FILE; } } } print "<br>End </body> </html> ";
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Directory Tree Help!
by aquarium (Curate) on Oct 21, 2010 at 01:57 UTC | |
by Anonymous Monk on Oct 21, 2010 at 02:13 UTC | |
by Anonymous Monk on Oct 21, 2010 at 21:20 UTC | |
by aquarium (Curate) on Oct 21, 2010 at 22:12 UTC | |
by Anonymous Monk on Oct 21, 2010 at 23:18 UTC |