Keep It Simple, Stupid | |
PerlMonks |
Re: m//g behaves strange...by Dominus (Parson) |
on Nov 10, 2003 at 20:01 UTC ( [id://305948]=note: print w/replies, xml ) | Need Help?? |
Says kokr:
In scalar context m//g doesn't return size of the list it would give back when called in list context,It's not supposed to do that. m//g in scalar context has a very interesting result: This prints:
2.The \G anchor is useless - perlre states, that it should be used only at the beginning of pattern,It's not useless. If you change the pattern in the example above to /\G\d+/g you get a different result. But here's a typical example of how one might use \G: This prints: What happens if you remove the 'useless' \G's? You get a very different result:
3.and most important - m//g in scalar context doesn't reset pos (matches only once).Here you have some confusion, but I can't tell what it is amongst the other confusions in your articles. Did you realize that the /./g in print "#7: ", /./g; was in list context, not scalar context? Did you realize that assigning $_ = "123" will reset pos($_)? Did you realize that m//g isn't supposed to 'reset' pos unless the match fails? In fact, the whole point of /g is that it does not reset pos. Ordinary matches, without /g, reset pos before matching begins. Consider this: The output is: So clearly pos is doing something. Now let's reset pos: Now the output is different: The first match is as before. But the pos($s) += 13 forces the current match position forward, into the middle of the 45, so that the next match sees only the 5 part. After matching the 5, the next pos($s) += 13 jumps past the 6 entirely, into the middle of the 789.
I ask you if it's proper behaviour / undocumented feature / bug? Or maybe I have missed something?A combination of all of these, I think. #1 is proper behavior. #2 seems to be a case of your having missed something. #3 is an undocumented feature, but it's undocumented because it doesn't exist. But also the behavior of \G and /g is very badly documented in general. I hope this helps, but I'm not sure what your objection is, so I can't address it directly.
In Section
Seekers of Perl Wisdom
|
|