One final thought :)
There is another way of doing "named captures", without using the construct or %+ or %-. It's a bit um ... obscure, and may be slower, but it might allow you to workaround the problem in the interim without radically altering your existing code.
#! perl -slw
use strict;
use re qw[ eval ];
use Data::Dump qw[ pp ];
my $reY = '(\d{4})(?{ $match{ y } = $^N })';;
my $reM = '(\d{2})(?{ $match{ m } = $^N })';;
my $reD = '(\d{2})(?{ $match{ d } = $^N })';;
my $reH = '(\d{2})(?{ $match{ h } = $^N })';;
my $reMN = '(\d{2})(?{ $match{ mn } = $^N })';;
my $reS = '(\d{2})(?{ $match{ s } = $^N })';;
my $reDT = "$reY-$reM-$reD\\s+$reH:$reMN:$reS";
our %match = ();
'2010-10-06 20:55:31' =~ $reDT;
pp \%match;;
__END__
c:\test>junk57.pl
{ d => "06", h => 20, "m" => 10, mn => 55, "s" => 31, "y" => 2010 }
Or better still, cut out the middleman and put the captures straight into the names variables themselves (I wish named captures worked this way full stop) :
#! perl -slw
use strict;
use re qw[ eval ];
use Data::Dump qw[ pp ];
my $reY = '(\d{4})(?{ $y = $^N })';;
my $reM = '(\d{2})(?{ $m = $^N })';;
my $reD = '(\d{2})(?{ $d = $^N })';;
my $reH = '(\d{2})(?{ $h = $^N })';;
my $reMN = '(\d{2})(?{ $mn = $^N })';;
my $reS = '(\d{2})(?{ $s = $^N })';;
my $reDT = "$reY-$reM-$reD\\s+$reH:$reMN:$reS";
local our( $y, $m, $d, $h, $mn, $s );
'2010-10-06 20:55:31' =~ $reDT;
print "$y, $m, $d, $h, $mn, $s";
__END__
c:\test>junk57.pl
2010, 10, 06, 20, 55, 31
Note: The variables referenced inside the (?{ code }) blocks have to be global, but judicious use of local and our makes it reasonably convenient. Also, I've had iffy results using qr// with this. Never really understood why.
I realise that it would be considerable work to modify all your regex generators to use this method, but hey!
You can always knock up a few regex to do it for you ;)
Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
"Science is about questioning the status quo. Questioning authority".
In the absence of evidence, opinion is indistinguishable from prejudice.
|