Yes, that is misunderstanding the role of the g modifier. Simplistically /g means "match as many times as you can". In a list context that means the regex will return all the matches it finds. Consider:
my @matches = '1 foo 22 bar 3' =~ /\d+/g; print "@matches";
Prints:
1 22 3
In scalar context however it returns true while there is a "next" match. To see what was matched we now have to capture the bit we are interested in:
while (my $match = '1 foo 22 bar 3' =~ /(\d+)/g) { print "$1 "; }
which generates the same output as above. Your code is rather like this last version except that you have "unwound" the loop.
To get the behaviour you expected without the /g you need to "anchor" the match at the start of the string using ^:
my @matches = '1 foo 22 bar 3' =~ /^\d+/g; print "@matches";
which prints '1'. For further regex reading see perlretut, perlre and perlreref.
In reply to Re^3: Multiple if statements matching part of one variable problem
by GrandFather
in thread Multiple if statements matching part of one variable problem
by jmclark
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |