Monks,

I currently looking at Data::Validator::Item to verify that the input data my application receives is according to specification. I am concerned about that the documentation says "alpha" and the module hasn't been updated since Jan 2003.

I wonder what (modules) the enlightened monks use to perform data validation.

use strict; use warnings; use Date::Calc qw(check_date); use Data::Validator::Item; my @variables = qw (FIRSTNAME GENDER BIRTHDATE_DDMMYYYY); my %dictionary = (); foreach my $variable (@variables) { $dictionary{$variable} = Data::Validator::Item->new(); $dictionary{$variable}->name($variable); $dictionary{$variable}->missing(q{}); # default blank } my $gender_coderef = sub { my $input = shift; my %transform = ( 1 => 'M', 2 => 'F', 3 => 'U', ); return $transform{$input}; }; my $ddmmyyyy_coderef = sub { my $ddmmyyyy = shift; $ddmmyyyy =~ /^(\d{2})(\d{2})(\d{4})$/; my ($year, $month, $day) = ($3, $2, $1); return check_date($year, $month, $day); }; $dictionary{GENDER}->transform($gender_coderef); $dictionary{GENDER}->values([ 1, 2, 3 ]); # Valid values *before* t +ransform $dictionary{BIRTHDATE_DDMMYYYY}->verify($ddmmyyyy_coderef); # ------ main ------ my $line = 0; while (<DATA>) { $line++; chomp; my ($index, $error) = (0, 0); my @fields = split(q{;}, $_); my @output = (); foreach my $elem (@fields) { if ($dictionary{ $variables[$index] }->validate($elem) != 1) { print "Error on line " . $line . " in column " . $variables[$index] . " value:" . $elem; $error++; } else { push @output, $dictionary{ $variables[$index] }->put($elem); } $index++; } # Print lines without errors only print join(q{;}, @output) if (!$error); } __DATA__ Kermit;1;21131986 Swedish Chef;3;21031959 Miss Piggy;4;07111987
Output:
$ perl -wl dvi.pl Error on line 1 in column BIRTHDATE_DDMMYYYY value:21131986 Swedish Chef;U;21031959 Error on line 3 in column GENDER value:4
--
Andreas

In reply to Alternatives to Data::Validator::Item by andreas1234567

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.