#!/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 "
Directory Tree 4
Starting Directory
";
MAIN:
{
my @tree;
dirwalk ($input_dir,\@tree);
#print "L32-\n";
printtree(\@tree);
#print "\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 "
\n";
if($arg_name eq "files") { # I am trying to insert code to call get_modules to find all the modules used in each Perl script here
get_modules($file_name);
#print "here";
}
}
}
print "
";
print "
";
}
sub get_modules
{
my ($file) = @_ ;
my (@all_files, @all_mod);
my $dir = "../../my_directory"; #this is the same as in $input_dir above
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 the directory
next unless ($file =~ m/(\.pl|\.pm)$/);
print "$file\n";
push @all_files, $file;
}
closedir(DIR);
if( ($arg_name eq "files") && ( $file_name ) ) {
print " Perl Modules used by $file_name: \n\n";
foreach my $files(@all_files) {
open (FILE, "$dir/$files");
while(my $line= ){
if(grep /\buse\b/, $line) {
if( ($files eq "$file_name") && ($line!~/^#/g) ) {
$line=~s/\buse\b//g;
print "$line";
}
}
}
close FILE;
}
}
}
print " End
";