title: How can I search multiple files for a string in Perl?
body: <p>My question is probably simple but I'm a complete newbie. I want to search the contents of multiple text files for a particular phrase and then display the lines of the finds on screen. I've already learnt how to deal with a single file. For example, if I want to search for a word, say "Okay" in a text file named "wyvern.txt" in the root directory of F. The following code works:</p>
  
  <pre><code>#!/usr/bin/perl
  
  $file = 'F:\wyvern.txt';
  open(txt, $file);
  while($line = &lt;txt&gt;) {
    print "$line" if $line =~ /Okay/;
  }
  close(txt);
  </code></pre>
  
  <p>But what should I do if I want to search for the same phrase in two text files, say "wyvern' and "casanova" respectively? or how about all the files in the directory "novels" in the root directory of F.</p>
  
  <p>Any help would be greatly appreciated.
  Thanks in advance</p>
  
  <p>Mike</p>
  
  <p><strong>Edit:</strong></p>
  
  <p>Haha, I finally figured out how to search all the files in a directory for a pattern match:)
  The following code works great:</p>
  
  <pre><code>#!/usr/bin/perl  
  @files = &lt;F:/novels/*&gt;;
  foreach $file (@files) {
  open   (FILE, "$file");
  while($line= &lt;FILE&gt; ){
  print "$line" if $line =~ /Okay/;
  }
  close FILE; 
  } 
  </code></pre>