in reply to Re: regex eval capture weirdness
in thread regex eval capture weirdness

GrandFather, minor quibble. Take the output section and change it to:

my @results = map { [$_ => domatch($_, $r)] } @strings; #foreach my $str (@strings) { foreach my $r (@results) { my ($str, $result) = @$r; print "Matching on $str\n"; #my $result = domatch($str,$r); if ($result) { print "Result:\n"; foreach my $k (keys %{$result}) { print "\t$k => $result->{$k}\n"; } } else { print "No match.\n"; } }
And notice it stop working. Well, notice it give the last result for all the matches. Now change your code to return { %mv } if keys %mv != 0 and notice it start working again.

It is bizarre ... and I'm hoping someone more familiar with the regexp engine internals will pipe up as to why this isn't DWIMming very well. I tried a number of different changes to try to get the engine to think I was doing another match, but failed, perhaps someone else will see what we're missing. In this way, I wouldn't (yet) call what you did a "fix" but more of a "workaround" ;-)

Replies are listed 'Best First'.
Re^3: regex eval capture weirdness
by GrandFather (Saint) on Dec 01, 2005 at 00:52 UTC

    Yes, "fix" was overstating the case somewhat in light of the level of understanding involved.

    Take a look the this discussion. It's the same problem. The issue is that the compilation of the (?{...}) in the regex creates a closure on %mv and the second time through the sub the my %mv; creates a different instance of %mv which is not the one the regex is using.

    Now I've told the teddy bear I understand the problem and why our works - at least until I get back to my desk. :)


    DWIM is Perl's answer to Gödel