in reply to While behavior
In the first example, you are calling m//g in list context. In the second example, you are calling it in scalar context. The different behaviours of m//g in list and scalar context are documented in perlop.
Relevant quote:
"The /g modifier specifies global pattern matching--that is, matching as many times as possible within the string. How it behaves depends on the context. In list context, it returns a list of the substrings matched by any capturing parentheses in the regular expression. If there are no parentheses, it returns a list of all the matched strings, as if there were parentheses around the whole pattern.
"In scalar context, each execution of m//g finds the next match, returning true if it matches, and false if there is no further match."
You're relying on the scalar context behaviour, but calling it in list context.
|
|---|