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.