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 = ) { 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; }