#!/usr/bin/perl -w # In my day job, we use PHP and I wanted to find all # the variables in a source tree that were used only # once, so I whipped this out. use strict; use File::Find; find( \&handler, "." ); my %byfile; my %all; my @listers = keys %byfile; # Comment this if you want all vars, not just those used once @listers = grep { $all{$_} <= 1 } @listers; for my $var ( sort @listers ) { print "$var\n"; my $files = $byfile{$var}; for my $file ( sort keys %$files ) { printf( " %5d %s\n", $files->{$file}, $file ); } # for file } # for var sub handler { if ( -d ) { $File::Find::prune = 1 if /^(CVS|pl)$/; return; } return unless /\.(html?|php|inc)$/; open( IN, $_ ) or die "Can't open $_: $!"; while () { s[//.+][]; # ignore comments (kinda) while ( /(\$[a-z_][a-z0-9_]*)/g ) { my $var = $1; ++$byfile{$var}{$File::Find::name}; ++$all{$var}; } # while grep } # while close IN; }