title: Perl: How do I search a text file line by line to find a specific pattern?
body: <p>I want to find a string <code>STRING</code> at the beginning of each line of a text file. So far, I have:</p>
  
  <pre><code>open(FILEHANDLE, "sample.txt")
  
  while(my $line = &lt;$FILEHANDLE&gt;){
      if($line =~ PATTERN){
         doSomething();
      }
  }
  </code></pre>
  
  <p>My question is: what do I put for <code>PATTERN</code> that will make my code work? I know that <code>STRING</code> will be at the beginning of each line of the text file, and that <code>STRING</code> will be followed by whitespace. <code>STRING</code> may change so it must be in a variable and not hardcoded in.</p>
  
  <p>So far, I can only think of the Python way to do this:</p>
  
  <pre><code>PATTERN = r"^" + re.escape(STRING) + r"\s"
  if re.search(PATTERN, line):
      doSomething();
  </code></pre>
  
  <p>How do I translate this to Perl? </p>
  
  <p>Also, if this is bad Perl syntax, please let me know so I can fix it.</p>