in reply to Directory Tree Help!

and what's the nature of the problem? i.e. what doesn't the program do correctly that you would like help with.
i did notice that you're looking for "use" surrounded by word boundary. not sure if you get a word boundary at the start of a line or not. in any case, you should be able to divide and conquer, by separating the grepping code into a little test script, and manually run it against example .pl files to watch output. if that works correctly, then the routine to generate filenames in the directory walk may need attention.
the hardest line to type correctly is: stty erase ^H

Replies are listed 'Best First'.
Re^2: Directory Tree Help!
by Anonymous Monk on Oct 21, 2010 at 02:13 UTC
    It runs ok, but once the directory tree expands and all the files listed there are displaying, these files needed to call the sub (by clicking) to get the modules that these files are using, and this is the part that doesn't work, I hope I made clear this time, thanks!

      Tip: "Doesn't work" doesn't work.

      We already know that it isn't doing what you wanted it to do (since you're having trouble or you wouldn't be here) so that phrase is useless.

      Instead, say what it actually did (copy and paste the result into code tags rather than typing it tho). Pointing out where things stopped matching what you expect is good too. Including the test results you expected is better.

      My personal preference for this problem would be to match
      /^\s*use\s+/
      or something along those lines. i'd use word boundary regexes when you need to tokenize whole lines of text into word units or similar. Have you tried at least to use your original regex without the initial word boundary, as per my earlier suggestion?
      if still not working..it's just a matter of running a single line of code with the regex, directly against any example .pl files you want to try. try and try again, adjusting the regex, until you get results you want. then put this regex in your script. sorry but i don't have regular access to perl as i type, at the moment.
      the hardest line to type correctly is: stty erase ^H
        Here it "WORKS", just put your directory name in line 13 and this code will list all .pl or .pm files in that directory, and if you click on the file name it will show all modules used by that file. It needs improvements, any help its appreciated. Next step could be to find any database table used by these files and listed there as well.
        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' ) || ''; my $file_name = $q->param( 'fname' ) || ''; my @file_path; print header(); print "<html> <head><title>Directory Tree 4</title> </head> <body> Start<br> "; 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 "<br>59 - $fullpath<br>"; if ( -d $fullpath ) { dirwalk ( $fullpath,\@{$tree->[@{$tree}]}); } else { push @{$tree},$item; #print "<br>$item<br>"; } } } sub printtree { my $tree = shift; my $c=-1; print "<ul><li><a href=\"#\">&nbsp;",shift @{$tree},"</a><br>\n"; foreach my $item ( @{$tree} ) { $c++; if (ref $item eq "ARRAY" ) { printtree($item); #if any subdirectories are found, call will +be here } else { print "<ul><li><a href=\"dir_tree.pl?fname=$item\">\n"; print $item,"</a></li></ul>\n"; if($file_name eq "$item") { get_modules($input_dir,$file_name,\@file_path); } } } print "</li>"; print "</ul>"; } 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= <FILE> ){ if(grep /\buse\b/, $file_line) { if( ($f_modules eq "$file_name") && ($file_line!~/^#/g) ) { #$file_line=~s/\buse\b//g; print "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp +;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$file_line<br>"; push @all_mod, $file_line; } } } close FILE; } } print "<br>End </body> </html> ";

        Thanks for the help!