in reply to /g matches not really global in scalar context!
This behaviour is intended and documented. Consider the following:
for ("I am a fish","I am a nonfish","I am a nonfish","fish I am a") { print "Start> $_\n"; if (/\GI\s*/g) { print "I\n"; if (/\Gam a\s*/g) { print "am a\n"; if (/\Gfish/g) { print "Yah! I am a fish\n"; } } } }
In other words, /g in a scalar context allows multiple distinct regexes to match after each other but from the place where the previous left off. This is very useful behaviour and is well worth familiarizing yourself with.
BTW, part of the reason that those constructs are marked experimental is because they allow sideeffects, which means that if the behaviour of the regex engine changes there is no guarantee the same side effects will occur. A good example is the following regex:
$_="aiutino aiutino aiutino fnorbley"; / \b aiutino \b \s* (?{ warn "Argh!" }) fnorbletrx /gix;
Note that it doesnt warn AT ALL. The reason is the longest fixed string in the pattern is 'fnorbletrx' but the string does not contain this pattern, and so the regex never runs as a regex. Which means the warn is never executed. In fact this pattern and this string is more like /.../ if instr($_,'fnorbletrx'); Its worth remembering this isa very common optimisation. Any regex that involves a mandatory constant string will automatically use this logic internally.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: /g matches not really global in scalar context!
by blazar (Canon) on Nov 14, 2005 at 13:03 UTC |