vansance has asked for the wisdom of the Perl Monks concerning the following question:

I am processing a form submission from a web page, and I am having trouble accepting '0' as a valid value in my regex.

The regex should accept all single digits in addition to '*' and '#'. This is the regex that I am using,
Readonly my $REGEX => qr/^\s*([0-9]|\*|\#)\s*$/;
Later on I test the message against the regex, and it works for all expected values, except for '0'.
if ( $message =~ $REGEX ) { do_something($1); } else { do_something_else(); }
I have tested this exact code as a command line perl script, and had no trouble with it at all. I have also put in all kinds of debug statements to see if anything funky is being attached to the form parameter, and i have stripped null, and all spaces just for good measure to no avail.

I have even gone so far as to hard code $message="0"; just to try and isolate the problem, and even that fails the condition.
I think that there must be something in my web app that is somehow changing the default behavior of the regex, but I have no idea what, and can't imagine what could be causing this?

I am running perl 5.8.8, Apache 2.2.6, and mod_perl in a linux environment. I am testing using firefox, and i admittedly have not tried it in another browser.

Any ideas? Thank you much.

Replies are listed 'Best First'.
Re: Weird mod_perl Regex '0' behavior.
by mr_mischief (Monsignor) on Sep 25, 2008 at 01:20 UTC
    This is just a guess, but since the docs for Readonly make no mention of working with Regexp references like what qr// returns your problem might be some interaction between that module and mod_perl. Have you tried it without the Readonly?

    If it is something to do with Readonly, you might be able to use the Readonly::Scalar1 procedure to mark just the reference itself as read-only. I'm not sure how the contents of a literal regex would change at a deeper level anyway, so Readonly trying to traverse and mark the referenced values shouldn't be necessary.

      I have tried all the suggestions with no luck.

      I have gone so far as to hard code this in, just to help figure out what is going on...
      my $zero = "0"; if ( $zero eq "0" ) { do_something(); } else { do_something_else(); }
      And it failed!

      I have two potential explanations.
      1. Somehow it is automatically evaluating any "0" string to false, which makes no sense to me.
      2. It is treating the input of "0" as an int, and not as a string, which also makes no sense to me.

      It couldn't even be some bug with the compiler, because I compile the same code as command line only and have no problems.

      Stumped and frustrated.
        Slow down. What you're saying here doesn't make any sense, so I think you must be missing something larger. Remeber, mod_perl doesn't change anything about perl except the life cycle of your program, so this code has to match the first if condition. Something else (configuration? other code before this? the do_something function?) is preventing you from seeing it. Or maybe this is not the real code you're running, in which case we can't help you if you don't show us the real code.
Re: Weird mod_perl Regex '0' behavior.
by GrandFather (Saint) on Sep 25, 2008 at 00:40 UTC

    Try changing your regex to qr/^\s*(?:[0-9]|\*|\#)\s*$/ - don't capture.


    Perl reduces RSI - it saves typing
      You do realize that the OP uses $1 in his code?

      The regex can be simplified in another way, though: qr/^\s*([0-9*#])\s*$/

        I suspect that the sample code posted by the OP is not his actual problem code. If the regex is actually in list context in the server side version of the code then he would see exactly the behaviour described. It seems to me that odd readonly effects as a function of run time input values in a mod_perl context only are much less likely than that the OP has "simplified" the code for us in some way that has removed an important part of the context.


        Perl reduces RSI - it saves typing