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.
RIP an inspiration; A true Folk's Guy

In reply to Re^16: Memory leak question by BrowserUk
in thread Memory leak question by SBECK

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.