title: In Perl regex "find & replace", how to individually pick out each match result?
body: <p>I want to use Perl to perform "search and replace" on text file, and store each match result (as an element) into an array while the replacement is done. I tried this:</p>
  
  <pre><code>my $txt = "
  this is a statement //this is comment
  //this is a line of comment
  more statements //more comments
  ";
  
  ## foreach or while
  while ($txt =~ s/(\/\/.*?\n)/foo/gs) {
          if(defined $1) {
                  push (@comments, $1);
                  }
          }
  
  foreach (0..$#comments) {
          print "\@comments[$_]= @comments[$_]";
          }
  </code></pre>
  
  <p>====> However the result only gives me:</p>
  
  <pre><code>@comments[0]= //more comments
  </code></pre>
  
  <p>Whereas, what I expect is:</p>
  
  <pre><code>@comments[0]= //this is comment
  @comments[1]= //this is a line of comment
  @comments[2]= //more comments
  </code></pre>
  
  <p>Any hints on the issue? Thanks &amp; 3q in advance~</p>