title: Perl, match one pattern multiple times in the same line delimited by unknown characters
body: <p>I've been able to find similar, but not identical questions to this one. How do I match one regex pattern multiple times in the same line delimited by unknown characters?</p>
  <p>For example, say I want to match the pattern HEY. I'd want to recognize all of the following:</p>
  <blockquote>
  <p>HEY</p>
  <p>HEY HEY</p>
  <p>HEYxjfkdsjfkajHEY</p>
  </blockquote>
  <p>So I'd count 5 HEYs there. So here's my program, which works for everything but the last one:</p>
  <pre><code>open ( FH, $ARGV[0]);
  while(&lt;FH&gt;)
  {
    foreach $w ( split )
    {
        if ($w =~ m/HEY/g)
        {
              $count++;
        }
    }
  }
  </code></pre>
  <p>So my question is how do I replace that foreach loop so that I can recognize patterns delimited by weird characters in unknown configurations (like shown in the example above)?</p>
  <p><b>EDIT:</b></p>
  <p>Thanks for the great responses thus far. I just realized I need one other thing though, which I put in a comment below.</p>
  <blockquote>
  <p>One question though: is there any way to save the matched term as well? So like in my case, is there any way to reference $w (say if the regex was more complicated, and I wanted to store it in a hash with the number of occurrences)</p>
  </blockquote>
  <p>So if I was matching a real regex (say a sequence of alphanumeric characters) and wanted to save that in a hash.</p>