I think this is what you're looking for. TIMTOWTDI, of course.
The STRING_TO_ID thing is a little dirty, but is one way of returning clean id's in your results. Also, I'm not sure of the best way to avoid regex characters in the string matches. I guess you could add in \Q and \E in init_matches().
use strict;
use warnings;
use CGI;
use File::Find;
use Data::Dumper;
use vars qw(@DIRS $ROOT_DIR %ID_TO_STRING %STRING_TO_ID);
$ROOT_DIR = "/some/path/etc";
@DIRS = qw
(
Admin
Backuprequests
Magic Network
NewServer
PatchManagement
Security
Serverlist
ServerRetire
);
# make sure vals are lowercase, to do a reverse lookup later on
%ID_TO_STRING =
(
c_strict => "use strict;",
c_warnings => "use warnings;",
c_cgi => "use cgi",
c_dbi => "use dbi",
);
# so we can get a clean id from the match
%STRING_TO_ID = reverse %ID_TO_STRING;
main();
sub main
{
my $match = init_match();
my $dirs = init_dirs();
my %script = ();
my $wanted = sub
{
my $path = $File::Find::dir . "/" . $_;
if (-f $path)
{
my %matches = ();
open(FILE, "<$path") || die $!;
while (my $line = <FILE>)
{
while ($line =~ m/($match)/ig)
{
# store results as hash keys so we only get one of
+ each
$matches{$STRING_TO_ID{lc $1}} = 1;
}
}
close FILE;
$script{$path} = [ keys %matches ];
}
};
find($wanted, @$dirs);
# output to log
warn Dumper \%script;
}
sub init_dirs
{
my $cgi = new CGI;
my $dirs = [ $cgi->param('dirs') ];
$dirs->[0] ||= "";
@$dirs = @DIRS if $dirs->[0] eq "ALL";
my @fullPaths = map { "$ROOT_DIR/$_" } @$dirs;
return \@fullPaths;
}
sub init_match
{
my $cgi = new CGI;
my @matches = ();
for my $sel (keys %ID_TO_STRING)
{
push @matches, $ID_TO_STRING{$sel} if $cgi->param($sel);
}
return join "|", @matches;
}
Of course, it's a good idea to research anything that you don't understand before using it.