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

In reply to regex corrupting separate regex by boxofrox

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.