Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Software: ActivePerl on Windows XP
Scenario: I have a list of strings to search in a folder and subfolders.
1. The list of strings is in a text file, say mylist.txt
2. I have a Perl script called search.pl that searches a specific string in a folder and subfolders. this works and is executed at the DOS command line as below: perl search.pl -pattern mysearchstring -ignorecase c:\inetpub\wwwroot\iprt
3. I need help to write a new Perl script that reads mylist.txt and for each string read in, it will feed that string to the search.pl script when calling it.
For example
mylist.txt has:
STRINGA
STRINGTEXTB
etc...
This new script will call search.pl passing STRINGA as pattern to search.
I think I can look up the tutorial to open mylist.txt and loop through its records.
What I don't know is how to call search.pl passing what is read in as string to search?
Search.pl is to be called with the same flags for each search string, same folder but the search string is parametized based on what is read in from mylist.txt.
Syntax for search.pl
search.pl -pattern <<searchparm>> -ignorecase c:\inetpub\wwwroot\iprt
Thank you and pardon my ignorance with Perl.
  • Comment on Can a Perl Script call another perl script?

Replies are listed 'Best First'.
Re: Can a Perl Script call another perl script?
by chargrill (Parson) on Jan 28, 2006 at 00:37 UTC

    This might help:

    perl -ne 'system( "perl search.pl -pattern $_ -ignorecase c:\\inetpub\ +\wwwroot\\iprt")' mylist.txt

    Note, you can just run it from the command line.



    --chargrill
    $/ = q#(\w)# ; sub sig { print scalar reverse join ' ', @_ } + sig map { s$\$/\$/$\$2\$1$g && $_ } split( ' ', ",erckha rlPe erthnoa stJu +" );
      Good answer, chargrill(++), but a Windows-based perl noob may have trouble translating the quotes. Here is the Windows command-line equivalent:
      perl -ne "system qq[perl search.pl -pattern $_ -ignorecase c:\\inetpub +\\wwwroot\\iprt]" mylist.txt
      FYI: the "-ne" makes perl read the file, and pass each record as "$_" into the code specified, looping until the file is read. Hence, the perl code in the square brackets gets executed for each line of the input file.

           "For every complex problem, there is a simple answer ... and it is wrong." --H.L. Mencken

        Huh. Granted, it's been a long time since I've Perled on windows, but I thought it would be OK with the quotes as written.

        Slight correction, it's just the -n that does that, the -e lets you specify the 'script' in its entirety on the command line.

        I was leaving the explanation of -ne as an exercise to the reader, or in this case the OP.



        --chargrill
        $/ = q#(\w)# ; sub sig { print scalar reverse join ' ', @_ } + sig map { s$\$/\$/$\$2\$1$g && $_ } split( ' ', ",erckha rlPe erthnoa stJu +" );
        Many thanks to CharGrill and Netwallah. The command works well.
      Many thanks to you and Netwallah. The command works well.
Re: Can a Perl Script call another perl script?
by holli (Abbot) on Jan 28, 2006 at 11:18 UTC
    Along with the ActiveState distribution comes a tool named search that does just what you need. To use a file as input for that program you can simply use a shell command:
    for /f %f in (mylist.txt) do search -name -i *.html -dir c:\intetput\w +wwroot\iprt %f
    Look up the docs for for and search by typing for /? and search -help at the command line.

    Edit:
    There is a trap when it comes to using for loop constructs in batch files. You have to "escape" the % signs in the loop variable (the batch interpreter eats one of them), so above line becomes
    for /f %%f in (mylist.txt) do search -name -i *.html -dir c:\intetput\ +wwwroot\iprt %%f


    holli, /regexed monk/
      I ran this command at the DOS prompt:
      for /f %f in (mylist.txt) do search -name -i *.* -dir D:\Temp\PPM9422 %f
      and got this error. What does it mean?
      It looks like it reads the values from mylist.txt but is expecting something else to do the search. Thanks
      D:\Temp\PPM9422>for /f %f in (mylist.txt) do search -i *.* -dir D:\Temp\PPM9422 %f
      D:\Temp\PPM9422>search -i *.* -dir D:\Temp\PPM9422 jury
      C:\Perl\bin\search.bat internal error: Quantifier follows nothing in regex; marked by <-- HERE in m/ * <-- HERE .*/ at (eval 3) line 238.
      D:\Temp\PPM9422>search -i *.* -dir D:\Temp\PPM9422 MSDN
      C:\Perl\bin\search.bat internal error: Quantifier follows nothing in regex; marked by <-- HERE in m/ * <-- HERE .*/ at (eval 3) line 238.
      where search.bat line 238 reads
      if ($arg =~ m/^-(i?)(d?)skip$/) {
      local($i) = $1 eq 'i';
      local($d) = $2 eq 'd';
      $! = 2, die qq/$0: expecting glob arg to -$arg\n/ unless @ARGV;
      foreach (split(/\s+/, shift @ARGV)) {
      if ($d) {
      $idskip{$_}=1 if $i;
      $dskip{$_}=1;
      } else {
      $iskip{$_}=1 if $i;
      $skip{$_}=1;
      }
      } next;
      I did the search -help and could not figure it out. D:\Temp\PPM9422>search -help
      usage: C:\Perl\bin\search.bat options -e PerlRegex ....
      OPTIONS TELLING *WHERE* TO SEARCH:
      -dir DIR start search at the named directory (default is current dir).
      -xdev stay on starting file system.
      -sort sort the files in each directory before processing.
      -nolinks don't follow symbolic links.
      OPTIONS TELLING WHICH FILES TO EVEN CONSIDER:
      -mtime # consider files modified > # days ago (-# for < # days old)
      -newer FILE consider files modified more recently than FILE (also -older)
      -name GLOB consider files whose name matches pattern (also -regex).
      -skip GLOB opposite of -name: identifies files to not consider.
      -path GLOB like -name, but for files whose whole path is described.
      -dpath/-dregex/-dskip versions for selecting or pruning directories.
      -all don't skip any files marked to be skipped by the startup file.
      -x<SPECIAL> (see manual, and/or try -showrc).
      -why report why a file isn't checked (also implied by -vvvv).
      OPTIONS TELLING WHAT TO DO WITH FILES THAT WILL BE CONSIDERED:
      -f | -find just list files (PerlRegex ignored). Default is to grep them.
      -ff | -ffind Does a faster -find (implies -find -all -dorep)
      OPTIONS CONTROLLING HOW THE SEARCH IS DONE (AND WHAT IS PRINTED):
      -l | -list only list files with matches, not the lines themselves.
      -nice | -nnice print more "human readable" output.
      -n prefix each output line with its line number in the file.
      -h don't prefix output lines with file name.
      -u also look "inside" manpage-style underlined text
      -i do case-insensitive searching.
      -w match words only (as defined by perl's \b).
      OTHER OPTIONS: -v, -vv, -vvv various levels of message verbosity.
      -e end of options (in case a regex looks like an option).
      -showrc show what the rc file sets, then exit.
      -norc don't load the rc file.
      -dorep check files with multiple hard links multiple times.
      Use -v -help for more verbose help.
      This script file is also a man page.
        What is the content of "mylist.txt"?
        for /f %f in (mylist.txt) do search -name -i *.* -dir D:\Temp\PPM9422 +%f
        is wrong. That must read
        for /f %f in (mylist.txt) do search -name *.* -i -dir D:\Temp\PPM9422 +%f
        or if you want to search for *.* then you can omit the -name parameter:
        for /f %f in (mylist.txt) do search -i -dir D:\Temp\PPM9422 %f


        holli, /regexed monk/