in reply to Dependencies, or, How Common is Regexp::Common?

How reliable do you want it to be? Because right now, in this simple case, you have some major errors. For example, with this snippet:
/^integer$/ and return qr/\d+/;
your routine would validate "foo123abc" as a valid value for an integer.

There's more to it than just being too lazy to install a module, you know. On the other side of the spectrum, there's the laziness of being pretty sure your module will do what you want it to do. A lot of work has been going into construction of these modules. At least, borrow some of that work, copying some of the code into your scripts, instead of reinventing a likely majorly flawed wheel yourself.

You may think that you have just made a minor error, and that this won't happen to you again. Think twice. These kinds of errors are all too common.

Replies are listed 'Best First'.
Re: Re: Dependencies, or, How Common is Regexp::Common?
by tachyon (Chancellor) on Sep 17, 2003 at 07:31 UTC

    We had a good one like that a while back. Essentially the code was like this (used to check client ID numbers)

    sub is_integer { return 0 unless $_[0]; return $_[0] =~ m/^\d$/ ? 1 : 0; }

    In development this routine was never required to deal with a TWO digit integer as all the developers used accounts with a <10 client ID number. Oh and the Test code.....the guy that wrote it tested all these args: undef,'', 'I am not an integer 42!', 0,1,2,3,4,5,6,7,8,9. Why ten tests for single digit integers and no tests for 16, 256,65535 GOK. Just goes to show that the volume of test is not the most important thing. Testing all the possible cases is. Even most of the probable cases would have been fine in this case.

    So you can guess what happens. During a live demo client 10 gets created, but client ID 10 is not an integer according to the sub. End of that demo. Much egg on developer and manager faces. And the bug (besides the inadequate test suite) a single missing +

    cheers

    tachyon

    s&&rsenoyhcatreve&&&s&n.+t&"$'$`$\"$\&"&ee&&y&srve&&d&&print

Re: Dependencies, or, How Common is Regexp::Common?
by legLess (Hermit) on Sep 17, 2003 at 07:02 UTC

    The error is in your assumption of how the code is used. The routine I gave doesn't do any validation at all, it just returns a quoted regex which is used elsewhere in the code to properly untaint incoming data:

    # $value set to incoming parameter earlier # $param object initialized earlier my $valex = $param->valex; $value =~ /($valex)/; $param->errors( "Parameter '" . $param->name . "' contained invalid data." ) unless(( defined $1 ) and ( $value eq $1 )); $param->value( $1 );

    Just for the heck of it I added another test, passing a parameter called 'bart' with a value of 'foo123abc'. I defined 'bart' as an integer and watched this test pass:

    is( $valop->value( 'bart' ), 123, 'foo123abc validated correctly' );

    Thanks for the reply, though.

    UPDATE: I should note that one of the simplifications I made (not expecting the Spanish Inquisition, as they say) to the code in my original post was changing this line in the module:

    $_ = shift || $self->validator;

    to:

    $_ = shift;

    In the module, &valex serves a dual purpose which I didn't think pertinent to the question I was asking.