0: #!/usr/bin/perl -w
1:
2: # In my day job, we use PHP and I wanted to find all
3: # the variables in a source tree that were used only
4: # once, so I whipped this out.
5:
6: use strict;
7: use File::Find;
8:
9: find( \&handler, "." );
10:
11: my %byfile;
12: my %all;
13:
14: my @listers = keys %byfile;
15:
16: # Comment this if you want all vars, not just those used once
17: @listers = grep { $all{$_} <= 1 } @listers;
18:
19: for my $var ( sort @listers ) {
20: print "$var\n";
21: my $files = $byfile{$var};
22: for my $file ( sort keys %$files ) {
23: printf( " %5d %s\n", $files->{$file}, $file );
24: } # for file
25: } # for var
26:
27: sub handler {
28: if ( -d ) {
29: $File::Find::prune = 1 if /^(CVS|pl)$/;
30: return;
31: }
32:
33: return unless /\.(html?|php|inc)$/;
34:
35: open( IN, $_ ) or die "Can't open $_: $!";
36: while (<IN>) {
37: s[//.+][]; # ignore comments (kinda)
38: while ( /(\$[a-z_][a-z0-9_]*)/g ) {
39: my $var = $1;
40: ++$byfile{$var}{$File::Find::name};
41: ++$all{$var};
42: } # while grep
43: } # while <IN>
44: close IN;
45: }
In reply to PHP variable cross-reference by petdance
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |