http://www.perlmonks.org?node_id=960264

shajiindia has asked for the wisdom of the Perl Monks concerning the following question:

Hi Monks, I am using the following code and it gives a wrong result when run. I am trying to figure out but could not get the reason. Please help.
$_ = "Twinkle twinkle little star"; $n = m/twinkle/ig; print "Number of occurences = $n\n"; while(/twinkle/ig) { $count++; } print "Found $count times\n";
When I run, it produces the following result

Number of occurences = 1
Found 1 times


but when I comment out lines 3 & 4 it gives the correct result. Why does the while loop produces the correct result when I comment out lines 3 & 4?

Found 2 times