Yesterday my friend Allan mailed me a script, he could not understand did not work for me to have a look at.
After debugging using the Perl debugger, trying all kind of tricks and reading some documentation, I started to suspect a bug in Perl, when I later talked to him, he had made an even smaller version of the and he mailed it to me.
I ended up writing my own barebones version of the demo, which demonstrates the problem:
use strict;
my $str = q( J:somestring I:someother );
my $i = 0; my $err = "\t### ERR ###\n\tNo match\n"; my $success = "\tsuccess...\n";
foreach my $key ("J","I") { my $str2 = $str;
print "### RUN 1/".++$i." ###\nTesting \$str ... looking for $key\n"; if ($str =~ m/^$key:.+/gm) { print "$success\n"; } else { print "$err\n"; }
print "Testing \$str2 ... looking for $key\n"; if ($str2 =~ m/^$key:.+/gm) { print "$success\n"; } else { print "$err\n"; } }
foreach my $key ("I","J") { my $str2 = $str;
print "### RUN 2/".++$i." ###\nTesting \$str ... looking for $key\n"; if ($str =~ m/^$key:.+/gm) { print "$success\n"; } else { print "$err\n"; }
print "Testing \$str2 ... looking for $key\n"; if ($str2 =~ m/^$key:.+/gm) { print "$success\n"; } else { print "$err\n"; } }
### RUN 1/1 ### Testing $str ... looking for J success...
Testing $str2 ... looking for J success...
### RUN 1/2 ### Testing $str ... looking for I success...
Testing $str2 ... looking for I success...
### RUN 2/3 ### Testing $str ... looking for I ### ERR ### No match
Testing $str2 ... looking for I success...
### RUN 2/4 ### Testing $str ... looking for J success...
Testing $str2 ... looking for J success...
In scalar context, each execution of "m//g" finds the next match, returning true if it matches, and false if there is no further match. The position after the last match can be read or set using the pos() function; see the pos entry in the perlfunc manpage. A failed match normally resets the search position to the beginning of the string, but you can avoid that by adding the "/c" modifier (e.g. "m//gc"). Modifying the target string also resets the search position.