Hi.

Been doing some work to parse some kinda-of-log files and in doing so I just got the idea that 5.10 named capture buffers in regex are exactly what I need to get a quick "give me a category for an error depending on which part of this regex matched" thing.

So got to work on it and ... weird results happened. See following code demonstrating it:

#!/usr/bin/perl -l use Getopt::Std; getopts( "fq", \%opt ); $line="ERROR 02052008-14:45 EDW08B50 ...ocal/log2000/data/incoming/bk/xp/bk005466.edi B: 1195043779 , RFT: AD0010043543 ERROR 02052008-14:45 EDW08B50 Closing date before today:2008-04-23"; $named_regex = '(?<DCSE>AD[^A]|NA|VN3)\S*'; $named_regex.= '$' if defined($opt{f}); $named_regex=qr/$named_regex/ if defined($opt{q}); $line=~/$named_regex/oms; print join("=>",%+);

Simply put, if you call this program without arguments, it goes and uses string in $name_regex on the match. If you run it with -q, it first pass that string through qr//. The -f controls if the regex ends with a $ or not.

Should give always the same result right? It doesnt:

# ./test.pl DCSE=>AD0 # ./test.pl -q DCSE=>AD0 # ./test.pl -f DCSE=>AD0 # ./test.pl -fq #

Somehow, that final $ on the strings means that, if $named_regex is just interpolated on the match, everything is ok, but if it is compiled as a regex first with qr//, then it isnt.

Any idea? From "it is evident that you are making a huge mistake, see..." from "better report it", anything is welcome


In reply to Weird bug with qr// and named capture buffers? Or just me? by Latro

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.