Latro has asked for the wisdom of the Perl Monks concerning the following question:
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
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Weird bug with qr// and named capture buffers? Or just me?
by almut (Canon) on Jun 17, 2008 at 17:13 UTC | |
|
Re: Weird bug with qr// and named capture buffers? Or just me?
by ikegami (Patriarch) on Jun 17, 2008 at 18:44 UTC | |
by Latro (Novice) on Jun 18, 2008 at 05:31 UTC | |
by ikegami (Patriarch) on Jun 18, 2008 at 14:21 UTC | |
by packy (Initiate) on Jun 09, 2009 at 19:52 UTC |