title: Perl Regex - Get offset of all the matches instead of one
body: <p>I want to search a file for a string and then get offsets for all the matches.
  The content of file is as below:</p>
  
  <pre><code>sometext
  sometext
  AAA
  sometext
  AAA
  AAA
  sometext
  </code></pre>
  
  <p>I am reading this whole file into a string <code>$text</code> and then doing a regex match for <code>AAA</code> as follows:</p>
  
  <pre><code>if($text =~ m/AAA/g) {
      $offset = $-[0];
  }
  </code></pre>
  
  <p><strong>This will give offset of only one <code>AAA</code>. How can I get offset of all the matches?</strong></p>
  
  <p>I know that we can get all matches in an array using syntax like this:</p>
  
  <p><code>my @matches = ($text =~ m/AAA/g);</code></p>
  
  <p>But I want offset not matched string.</p>
  
  <p>Currently I am using following code to get offsets of all matches:</p>
  
  <pre><code>my $text= "sometextAAAsometextAAA";
  my $regex = 'AAA';
  my @matches = ();
  
  while ($text =~ /($regex)/gi){
      my $match = $1;
      my $length = length($&amp;);
      my $pos = length($`);
      my $start = $pos + 1;
      my $end = $pos + $length;
      my $hitpos = "$start-$end";
      push @matches, "$match found at $hitpos ";
  }
  
  print "$_\n" foreach @matches;
  </code></pre>
  
  <p>But is there a simpler way to to this?</p>