Searches through all the program source files in a directory searching for functions and variables, then tables them at the end of the search. Doesn't recurse directories, and the regexps are rather weak to say the least. Designed for php files, adapted (poorly) for perl files. Posted in response to another monks need so don't rip me too hard (except about the regexps, that's fair).
#/usr/bin/perl -w #By Jepri of www.perlmonks.com use strict; use diagnostics; my $cd = $ARGV[0]; #The directory to be scanned #my $cdh; #Directory handle. Shame I don't use HURD my @dc; #Directory contents. A list of files in dir #my $i; #Loop counter my %fl; #Function locations (which files are they in?) my $ft; #The type of file we are examining # Regexps to try and detect a function name by catching # 'function' or 'sub' declaration. Keyed by language my %search=('php','(UNCTION|unction)\s(.*?)(\(|\s*\()(.*?)\)','perl', +'(sub)\s(.*?)(\{|$)'); #ft is filetype, a hash of the file languages and the extension to ide +ntify them my %ft=('php', '\.(php|php.)$', 'c', '\.c$', 'perl', '(\.pl.?|\.pm)$') +; my %vars; #A cheap way of maintaining a list so that each i +tem is unique my ($field1, $field2); $~="ALT"; opendir(CDH, $cd); @dc=readdir(CDH); closedir CDH; FILE: foreach my $i (@dc){ #Try to detect the type of file we are examining $ft='none'; #Default (fall through) option foreach my $j ( keys %ft ) {$ft=$j if $i=~ /$ft{$j}/;} #Bail if we couldn't figure it out. next FILE if $ft eq 'none'; open(FH, "<$cd$i") || die "can't open file $cd$i: $!"; #Go through the file and try to pick up functions and #variables. Print out the function declarations #immediately, push the functions and variables #into HOL to be printed out at the end. print "\n\nThe routines defined in $i\nvvvvvvvvvvvvvvvvvvvvvvvvvvv +vvvvvvvvv\n"; while (<FH>){ $field1=''; if (/$search{$ft}/ && $2){ $field1="$2"; &add_func($2,$i); $field2= " requires $4" if $4; $field2= " has no arguements" unless $4; } if (/return /){$field2="$field2 and returns a value";} #Horrible regexp to try and detect a varible #Spank me please if (/(\$.*?)(\s|\-|\$|\%|\@|\{|\}|\)|\:|\(|\,|\;|\<|\+|\"|\\|\ +}|\[|\{)/g) { $vars{$1}{$i}=' '; #push @{$vars{$1}},' '; } write if $field1; } close FH; } print "\n\nBelow is a list of all the functions calls found, and then +the files they were found in\n"; foreach my $i ( keys %fl ) { $field1=$i; $field2=join(' ', @{ $fl{$i} }); write; } print "\n\nBelow is a list of all variable names found\n"; foreach $field1 ( keys %vars ) { #print "$family : "; $field2=''; for my $role ( keys %{ $vars{$field1} } ) { #print "$role "; $field2="$field2, $role"; } write(); } sub add_func(){ my $func=$_[0]; my $file=$_[1]; push @{ $fl{$func} },$file; } format ALT = @<<<<<<<<<<<<<<< ^<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< +<<<<<< $field1, $field2 ~~ ^<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< +<<<<<< $field2; .