#!/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' ) || '';
my $file_name = $q->param( 'fname' ) || '';
my @file_path;
print header();
print "
Directory Tree 4
Start
";
MAIN:
{
my @tree;
dirwalk ($input_dir,\@tree);
printtree(\@tree);
}
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";
push @file_path, $fullpath;
#print "
59 - $fullpath
";
if ( -d $fullpath )
{
dirwalk ( $fullpath,\@{$tree->[@{$tree}]});
}
else
{
push @{$tree},$item;
#print "
$item
";
}
}
}
sub printtree
{
my $tree = shift;
my $c=-1;
print "- ",shift @{$tree},"
\n";
foreach my $item ( @{$tree} )
{
$c++;
if (ref $item eq "ARRAY" )
{
printtree($item); #if any subdirectories are found, call will be here
}
else
{
print "\n";
if($file_name eq "$item") {
get_modules($input_dir,$file_name,\@file_path);
}
}
}
print " ";
print "
";
}
sub get_modules
{
my $dir = $_[0];
my $get_files = $_[1];
my @fullpath = @{$_[2]};
my @just_path;
foreach my $just_path(@fullpath) {
if($just_path=~/(.*?)(\/$get_files)(.*?)/g) {
$just_path=~s/(.*?)(\/$get_files)(.*?)/$1/;
$just_path=$1;
push @just_path, $just_path;
}
}
my $path = shift(@just_path);
my (@all_files, @all_mod);
opendir DIR, $path || die "Couldnt open $path - $!\n";
while (my $find_file = readdir(DIR)) {
next unless (-f "$path/$find_file");
# Use a regular expression to find files ending in .pl
next unless ($find_file =~ m/(\.pl|\.pm)$/);
push @all_files, $find_file;
}
closedir(DIR);
foreach my $f_modules(@all_files) {
open (FILE, "$path/$f_modules");
while(my $file_line= ){
if(grep /\buse\b/, $file_line) {
if( ($f_modules eq "$file_name") && ($file_line!~/^#/g) ) {
#$file_line=~s/\buse\b//g;
print " $file_line
";
push @all_mod, $file_line;
}
}
}
close FILE;
}
}
print "
End
";