title: How can I find the location of a regex match in Perl?
body: <p>I need to write a function that receives a string and a regex. I need to check if there is a match and return the start and end location of a match. (The regex was already compiled by <code>qr//</code>.)</p>
  
  <p>The function might also receive a "global" flag and then I need to return the (start,end) pairs of all the matches.</p>
  
  <p>I cannot change the regex, not even add <code>()</code> around it as the user might use <code>()</code> and <code>\1</code>. Maybe I can use <code>(?:)</code>.</p>
  
  <p>Example: given "ababab" and the regex <code>qr/ab/</code>, in the global case I need to get back 3 pairs of (start, end).</p>