Beefy Boxes and Bandwidth Generously Provided by pair Networks
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??


in reply to m//g behaves strange...

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:
my $s = "123 45 6 789"; while ($s =~ m/\d+/g) { print "> $&\n"; }
This prints:
> 123 > 45 > 6 > 789
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:
my $s = "123 carrots 45 6 bananas 789"; while (1) { $s =~ /\G(\d+)/gc and print "NUMBER $1\n" and next; $s =~ /\G\s+/gc and print "SPACE\n" and next; $s =~ /\G([a-z]+)/gc and print "WORD $1\n" and next; $s =~ /\G$/gc and last; }
This prints:
NUMBER 123 SPACE WORD carrots SPACE NUMBER 45 SPACE NUMBER 6 SPACE WORD bananas SPACE NUMBER 789
What happens if you remove the 'useless' \G's? You get a very different result:
NUMBER 123 NUMBER 45 NUMBER 6 NUMBER 789
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:

my $s = "123 carrots 45 6 bananas 789"; while ($s =~ /(\d+)/g) { print "'$1' at position ", pos($s)-length($1), "\n"; }
The output is:
'123' at position 0 '45' at position 15 '6' at position 18 '789' at position 29
So clearly pos is doing something. Now let's reset pos:
my $s = "123 carrots 45 6 bananas 789"; while ($s =~ /(\d+)/g) { print "'$1' at position ", pos($s)-length($1), "\n"; pos($s) += 13; }
Now the output is different:
'123' at position 0 '5' at position 16 '89' at position 30
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.

--
Mark Dominus
Perl Paraphernalia

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://305948]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others having a coffee break in the Monastery: (6)
As of 2024-03-19 11:30 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found