I have consecutive perl statements that each evaluate a regex, and it appears that the result of the previous regex corrupts the result of the next regex.
Below are two test cases showing this unexpected response.
The first test case mimics the original code that introduced this corruption.
The second mimics the work around I used to move past this issue and get my code working, yet the corruption occurs if I continue after the search matches.
The output prints the regex with values for the name search, and then for the related version search.
The variable values are enclosed in brackets to identify all characters in the string.
Q: Can anyone explain why this is happening and perhaps what perl thinks it's doing?
Test Case 1
#!/usr/bin/perl -w
use strict;
# create sample task list
my $tasks = [
[ "A", "1.1"],
[ "B", "1.2"],
[ "C", "2.4"],
[ "D", "2.9"],
[ "E", "1.3.1"],
];
my $task_name = "C"; # use to hold task name i'm search
+ing for
my $task_version = ""; # use to hold task version i'm sea
+rching for
my ($cache_name, $cache_version); # use to hold data from task list
my ($match_name, $match_version); # use to hold result of regex matc
+h
foreach my $array ( @$tasks ) {
$cache_name = $array->[0]; # get task name
$cache_version = $array->[1]; # get task version
# error occurs here
$match_name = ($cache_name =~ /^$task_name$/i) ? "match" : "no match
+";
$match_version = ($cache_version =~ /$task_version/i) ? "match" : "n
+o match";
info( 0, 24, "[$cache_name] =~ /^$task_name\$/i", $match_name );
info( 4, 24, "[$cache_version] =~ /$task_version/i", $match_version
+);
}
## Info Subroutine ##
sub info {
my $indent = shift;
my $width = shift() - $indent;
my $msg = shift;
my $result = shift;
my $margin = " " x $indent;
printf( "%s%-${width}s | %10s\n",
$margin,
$msg ? substr( $msg, 0, 60 ) : "",
$result ? substr( $result, 0, 10 ) : "" );
}
######## OUTPUT ########
[A] =~ /^C$/i | no match
[1.1] =~ //i | match
[B] =~ /^C$/i | no match
[1.2] =~ //i | match
[C] =~ /^C$/i | match
[2.4] =~ //i | no match <-- unexpected
[D] =~ /^C$/i | no match
[2.9] =~ //i | no match <-- unexpected
[E] =~ /^C$/i | no match
[1.3.1] =~ //i | no match <-- unexpected
Test Case 2
#!/usr/bin/perl -w
use strict;
# create sample task list
my $tasks = [
[ "A", "1.1"],
[ "B", "1.2"],
[ "C", "2.4"],
[ "D", "2.9"],
[ "E", "1.3.1"],
];
my $task_name = "C"; # use to hold task name i'm search
+ing for
my $task_version = ""; # use to hold task version i'm sea
+rching for
my ($cache_name, $cache_version); # use to hold data from task list
my ($match_name, $match_version); # use to hold result of regex matc
+h
foreach my $array ( @$tasks ) {
$cache_name = $array->[0]; # get task name
$cache_version = $array->[1]; # get task version
# reversing order of regex works around this bug
$match_version = ($cache_version =~ /$task_version/i) ? "match" : "n
+o match";
$match_name = ($cache_name =~ /^$task_name$/i) ? "match" : "no match
+";
info( 0, 24, "[$cache_name] =~ /^$task_name\$/i", $match_name );
info( 4, 24, "[$cache_version] =~ /$task_version/i", $match_version
+);
}
## Info Subroutine ##
sub info {
my $indent = shift;
my $width = shift() - $indent;
my $msg = shift;
my $result = shift;
my $margin = " " x $indent;
printf( "%s%-${width}s | %10s\n",
$margin,
$msg ? substr( $msg, 0, 60 ) : "",
$result ? substr( $result, 0, 10 ) : "" );
}
######## OUTPUT ########
[A] =~ /^C$/i | no match
[1.1] =~ //i | match
[B] =~ /^C$/i | no match
[1.2] =~ //i | match
[C] =~ /^C$/i | match
[2.4] =~ //i | match <-- EXPECTED
[D] =~ /^C$/i | no match
[2.9] =~ //i | no match <-- unexpected
[E] =~ /^C$/i | no match
[1.3.1] =~ //i | no match <-- unexpected