why the loop-break after pattern-matching

D

dn.perl

Here is the code:
#!/usr/local/bin/perl

use strict ;
use warnings ;

my $counter = 1;
my $log = "[pattern number-405] check" ;
print "log is:$log\n" ;
while ( $log =~ /(pattern)( \S+-\d+)+/ig )
{
my $match = $& ; ## $& is the str yielded by the last pattern
match
$match =~ s/^\S+ // ; # remove first word
print "match is $match\n" ;
last if $counter++ > 25 ;
}
print "log is:$log\n" ;
# - - - - - - - - - - -

Why does the loop break after just the first pass? I would expect the
while condition to never change in this patch.
 
D

dn.perl

while ( $log =~ /(pattern)( \S+-\d+)+/ig )
{
    last if $counter++ > 25 ;
}

print "val is:$counter\n" ;
# - - - - - - - -  - - -

Why does the loop break after just the first pass?

Well, apparently in a scalar context, the 'g' option remembers where
it had left off the pattern-matching the last time and takes it up
from that point. !!!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,754
Messages
2,569,526
Members
44,997
Latest member
mileyka

Latest Threads

Top