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