On 30 May 2007 08:53:54 -0700, Paul Lalli <[EMAIL PROTECTED]> wrote:
snip
I got confused by this too.  I think Sharan's question comes down to
"why isn't this an infinite loop?"  That is, why does pos() move ahead
one character when it matches 0 characters?  This is not limited to
look-ahead assertions.  The behavior can be seen in other constructs
as well.  For example:

$ perl -wle'
$string = "abc";
while ($string =~ /(.*?)/g) {
  print pos($string), ": ", $1;
}
'
0:
1: a
1:
2: b
2:
3: c
3:

Because /.*?/ matches nothing as well as a, b, and c.  So it matches
nothing, then a, then nothing, then b, then nothing, then c. then
nothing.


It appears that Perl is actually dividing the string up into
"characters" and "slots between character", and allowing pos() to move
to each of them in sequence.  So at the beginning, it's at the slot
before the first character, and it can successfully match 0
characters.  Then pos() moves to the first character, and the fewest
characters it can find is that one character, so $1 gets 'a'.  Then it
moves to the slot between 'a' and 'b'.  Etc.

Yes, otherwise \b wouldn't work very well.

perldoc perlre
   A word boundary ("\b") is a spot between two characters that has a "\w"
   on one side of it and a "\W" on the other side of it (in either order),
   counting the imaginary characters off the beginning and end of the string
   as matching a "\W".

snip
Here's another, that doesn't allow any characters to be matched:
$ perl -wle'
$string = "abc";
while ($string =~ /(.{0})/g) {
  print pos($string), ": ", $1;
}
'
0:
1:
2:
3:

Would the above be an accurate description of what's happening?  And
if so, is this behavior documented anywhere?  I couldn't find it in a
cursory examanation of either perlop or perlre...
snip

You are matching the nothing between the characters.

--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/


Reply via email to