About 10 months ago, as of this writing, a bug for Regexp::Common was entered regarding a lone decimal point matching the real number regex. Here's an example:

perl -MRegexp::Common -le 'print "." =~ $RE{num}{real}'

And that dutifully prints "1" because of the bug. I need to figure out how to get that regular expression to fail. But there's a problem. I need to pass the regular expression to another piece of code and it's important that I need to have that regex fail within the regular expression. In other words, I can't do this:

if ('.' ne $_ && /$RE{num}{real}/) { ... }

Since this bug was reported 10 months ago, I'm not sanguine about it being fixed any time soon and I need to solve this problem now. Either I can decide not to use the Regexp::Common regex for this one match or I can force the regex to fail. The former option is the one I'm going with, but I was curious as to why my code to force the regex to fail didn't work (note that what follows has some extra stuff, but it mirrors exactly what I need).

#!/usr/bin/perl use strict; use warnings; use Data::Dumper; use Regexp::Common; use constant SUCCEED => qr{(?=)}; use constant FAIL => qr{(?!)}; my $QUOTED = $RE{quoted}; my $NUM = $RE{num}{real}; my $VALUE = do { use re 'eval'; qr/(?:$QUOTED|$NUM)(??{'.' eq $+ ? FAIL : SUCCEED})/; }; my $text = 'name => "foo", fav.num => 3'; my @text = split /($VALUE)/ => $text; print Dumper \@text;

That prints:

$VAR1 = [ 'name => ', '"foo"', ', fav', '.', 'num => ', '3' ];

What I want it to print is:

$VAR1 = [ 'name => ', '"foo"', ', fav.num => ', '3' ];

Because of the nature of the code (using the lexer from Higher Order Perl's chapter 8, if you're curious), I can't change how the split line operates. I pass regexes to the split and I need the regex to fail at that time. Can anyone help fix the first regex or tell me why my attempt at failing it is broken? (I've also tried the $^N variable, but no love.) I'd prefer to fix my code as I'd like to rely on the Regexp::Common module, but I also know that I'm using experimental features.

Or maybe there's something really simple that I haven't seen.

Cheers,
Ovid

New address of my CGI Course.


In reply to Forcing a regex to fail by Ovid

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.