title: Perl iterate over the number of regexp matches
body: <p>I have some lines like</p>
  
  <pre><code>int foo[] = { a, b, c, d, e };
  </code></pre>
  
  <p>The exact numbers of elements in the array is unknown. I want to match it using <code>perl</code>'s regexp. I identify the lines using</p>
  
  <pre class="lang-perl prettyprint-override"><code>if ( $line =~ /^\ *int\ *([a-z]+)\ *\[\]\ *=\ *{(\ *([a-z]+)\ *,?)+}\ *;/ )
  {
    print $line;
  }
  </code></pre>
  
  <p><strong>How can I get the number of matches, as I want to work on each of the elements in the array?</strong></p>
  
  <p>Ideally, I would like something like</p>
  
  <pre class="lang-perl prettyprint-override"><code>if ( $line =~ /^\ *int\ *([a-z]+)\ *\[\]\ *=\ *{(\ *([a-z]+)\ *,?)+}\ *;/ )
  {
    my $count_of_matches = ??????
    for (my $matches=0 ; $matches&lt;count_of_matches ; $matches++)
    {
      print $matches, "\n";
    }
  }
  </code></pre>
  
  <p>which should result in something like</p>
  
  <pre><code>foo
  a
  b
  c
  d
  e
  </code></pre>
  
  <p>I tried</p>
  
  <pre class="lang-perl prettyprint-override"><code>my $count_of_matches = () = $line =~ /.../ # using the same pattern as above
  
  my @list  = $line =~ /.../
  my $other_count_of_matches = scalar @list
  </code></pre>
  
  <p>But I get a count of 3, and the <code>perl</code> array only contains the array name (<code>foo</code>) and twice the last element (<code>e</code>).</p>