Hi Monks!
I am working on this script to list all .pl and .pm files in a directory and later in all found directories. The code I have works until a given directory given list all files, but I am stuck getting to the file level to look for the Perl modules been used by each file found. I have the sub routine that will do that just having problem in make this code work with what I already have. Here is the code:
#!/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=\"#\">&nbsp;",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> ";

Thanks for the Help!

In reply to Directory Tree Help! by Anonymous Monk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.