in reply to variables in global match in regex

In order for m/<some stuff>/g to return multiple matches, you need to either evaluate it in a list context or repetitively evaluate the same match in scalar context.

The scalar match can be done with with a while loop, as shown in other replies. Note that the same string must be matched, so if you used

sub foo { $mystring } while (foo =~ m/blah../g) {

, you would get the first match over and over, since each time you are matching on a different copy of the string. The list context match can be done with foreach for iteration, e.g.:

$/=undef; for (<> =~ /\((.*?)\)/g) { print $1, "\n" }

while _repeatedly_ evaluates its condition in scalar context, foreach repeatedly executes its body for values of the list obtained by evaluating the argument _once_ in list context. Btw, this means you do not want to use foreach if your list is way too big for your memory...