Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?

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]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others exploiting the Monastery: (6)
As of 2018-06-18 10:23 GMT
Find Nodes?
    Voting Booth?
    Should cpanminus be part of the standard Perl release?

    Results (109 votes). Check out past polls.