Beefy Boxes and Bandwidth Generously Provided by pair Networks httptech
No such thing as a small change
 
PerlMonks  

Questions on File::Find

by hok_si_la (Curate)
on Sep 15, 2005 at 22:39 UTC ( #492451=perlquestion: print w/ replies, xml ) Need Help??
hok_si_la has asked for the wisdom of the Perl Monks concerning the following question:

Hello monks,

I supersearched some of the old File::Find related nodes and didn't find anything that really helped me understand my problem. I also skimmed over the docs a couple of times. I have a form that contains a muliple select called dirs, 7 checkboxes, and a text box. The form will invoke a script that will seach every file in every directory in dirs for occurences of certain strings. So for instance if my multiple select 'dirs' contains foo and bar, and use strict and use warnings are checked, then each File::Find::name is grep'ed for "use warnings" and "use strict". The results are returned to a hash of arrays. Example: %results(myscript.pl =>[warnings, strict]);.

I am trying to find out the best way to accomplish this algorithmically and how to use &wanted.
Here is the code I have so far...
############################################################ # This script displays all scripts containing certain params ############################################################ # Author: Monica Lewinski # Date: 09/32/2031 ############################################################ use strict; use CGI; use File::Find; require "VLO_NTlib.pl"; my $cgi = CGI->new; my $dirs = [ $cgi->param('dirs') ]; if ($dirs->[0] eq "ALL") { @$dirs = qw("Admin", "Backuprequests", "Magic", "Network", "NewSer +ver", "PatchManagement", "Security", "Serverlist", "ServerRetire"); } find(\&wanted, @$dirs); exit (0); ############################################################ sub wanted ############################################################ { my @wanted; my $i=0; if ($cgi->param('c_strict')){ $wanted[$i] = "use strict\;"; ++$i; } if ($cgi->param('c_warnings)){ $wanted[$i] = "use warnings\;"; ++$i; } if ($cgi->param('c_vlontlib')){ $wanted[$i] = "require \"VLO_NTlib.pl\"\;"; ++$i; } if ($cgi->param('c_cgi')){ $wanted[$i] = "use CGI"; ++$i; } if ($cgi->param('c_dbi')){ $wanted[$i] = "use DBI"; ++$i; } if ($cgi->param('c_tabs')){ $wanted[$i] = "PAGE1CLEAN"; ++$i; } if ($cgi->param('c_heredocs')){ $wanted[$i] = "END_OF_PRINT"; ++$i; } if ($cgi->param('contains')){ $wanted[$i] = $cgi->param('contains'); ++$i } }
Any suggestions to clarify my mess would be appreciated,
hok_si_la

Comment on Questions on File::Find
Select or Download Code
Re: Questions on File::Find
by revdiablo (Prior) on Sep 15, 2005 at 23:55 UTC

    I would move all the calls to $cgi->param outside of the wanted sub. That is executed for every file being iterated on; probably not what you want to do.

    Also, the only thing you're doing is checking if the CGI parameters are set, you're not doing anything with the files being iterated on. If you want to search for a string in the contents of each file, you'll have to open each file, then iterate on the file contents. Of course you'll want to do this only once per file, using open.

    If I were doing it, I would probably construct a big regex based on the selected search options, and check for a match of that regex as I iterated on each file's contents. But you have some flexibility here. Maybe with a more specific question we can be of more use?

Re: Questions on File::Find
by joecamel (Hermit) on Sep 16, 2005 at 00:09 UTC
    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().

    Of course, it's a good idea to research anything that you don't understand before using it.

    joecamel
      First, thanks joecamel and revdiablo for your assistance.

      I was mainly interested in finding an efficient algorithmic approach to the problem and the answers that both of you provided sufficed. I began coding and realized the problem was a bit more complicated than I originally expected. This is why the code I posted really didn't do anything. Hopefully I was finish tidying up the script over the weekend. I will keep you posted on my progress.

      hok_si_la

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://492451]
Approved by polettix
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others musing on the Monastery: (6)
As of 2014-04-20 07:15 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    April first is:







    Results (485 votes), past polls