Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses

Re: Efficient way to do field validation

by sundialsvc4 (Abbot)
on Aug 01, 2013 at 01:14 UTC ( #1047336=note: print w/replies, xml ) Need Help??

in reply to Efficient way to do field validation

When faced with tasks like this, I usually write a subroutine which, given a string (or whatever the input may be), is tasked with returning either “falsehood,” or, if any error is encountered, an appropriate error-message string.

I normally wrap the entire body of such a function in an eval {} block, which will trap any errors that may occur.   If an exception is thrown (via die or otherwise), the content of that exception string is returned; if not, falsehood.   I also often define a $doing_what variable that I set to appropriate strings as I run through the subroutine from top to bottom.   This value can be used to augment the messages.

And then... what can I say... you just go for it.   split() the string into an array, then check the number of entries in the array:   die() if the count is wrong.   Then, on to the next test.   And you simply run through them, one after another after another.

Now, one more thing:   welcome to the world of Test::More and Test::Exception!   You must not assume that your validation routine is, indeed, correct.   You need to write a very comprehensive test-suite that throws everything but Lincoln’s Gettysburg Address(*) at it.   This test suite should verify that the routine traps every error that it is supposed to, and that it validates every good string that it is supposed to.   This is a complex but vitally important routine, and you need to test it rigorously.

(*) Yes, there’s a story here .. apocryphal or otherwise I don’t know.   Legend has it that an early “error-correcting” COBOL(?) compiler, when given a copy of the aforesaid document, “compiled it” with no errors.

  • Comment on Re: Efficient way to do field validation

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1047336]
[stevieb]: yeah, so the wiringPi library appears to be missing/injecting incorrect defined variables into I2C calls, and it's all over the map. I'm going to have to revisit and use something else, write something else, or scrutinize the code and fix

How do I use this? | Other CB clients
Other Users?
Others perusing the Monastery: (3)
As of 2017-06-23 00:06 GMT
Find Nodes?
    Voting Booth?
    How many monitors do you use while coding?

    Results (532 votes). Check out past polls.