in reply to regex eval capture weirdness
It's a closure problem (which I don't understand well). The fix looks like:
sub domatch { my ($str, $regex) = @_; our %mv; # Note the our return 0 if $str !~ /$regex/; return \%mv if keys %mv != 0; print "No keys in hash.\n"; return 0; }
Which in the context of your sample code prints:
original pseudo-regex: test_%a%b modified regex: test_(\d{2})(?{$mv{a} = $^N })(\d{4})(?{$mv{b} = $^N } +) Matching on test_001022 Result: a => 00 b => 1022 Matching on test_585381 Result: a => 58 b => 5381 Matching on test_389742 Result: a => 38 b => 9742 Matching on test_330104 Result: a => 33 b => 0104
Neat trick with the (?{...$^N}) BTW.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: regex eval capture weirdness
by Tanktalus (Canon) on Dec 01, 2005 at 00:28 UTC | |
by GrandFather (Saint) on Dec 01, 2005 at 00:52 UTC | |
|
Re^2: regex eval capture weirdness
by bytex64 (Novice) on Dec 01, 2005 at 20:03 UTC |