I'm validating input to a function, via named parameters, that inserts stuff into a database using DBI. For each parameter passed (say %param), I run it through a bunch of validation routines, massage it and spit out what, if all tests pass, should be inserted into the database...my validation routines for each parameter might be different and are stored as references in a hash. ie.
%validationHash = ( "name" => { "regexp" => "string", "status" => "required", "outputsub" => \&massage; }, "phone" => { "regexp" => "phone", "status" => "optional", "outputsub" => \&mungephonenum, }, );
then I iterate over the keys in the hash (ie.$param{'name'}) (thereby ignoring any parameters passed that I don't care about), make sure it is defined if $validationHash{"name"}{"status"} eq "required" otherwise return undef etc. then make sure that it looks like a "string" with a routine that runs a regexp match and finally, return a value $validationHash{'name'}{'outputsub'}->($param{'name'}) as what should be inserted into the database.

Needless to say, I would love to hear about nice ways of validating input and even better, ways of constructing SQL statements given such input (I'm considering using DBIx::Recordset for the latter, but I've done enough work already that I might have to come back to it at another time).

BTW, the first solution given worked very well and I am using it successfully, thanks.
-Adi


In reply to Re: Re: How do I construct a reference to a builtin function? by adram
in thread How do I construct a reference to a builtin function? by adram

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.