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; .

In reply to Find varibles and function names in program files by jepri

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.