How about something like this...

use Type::Params qw(compile); use Types::XSD qw(String Decimal Date Integer); use Text::CSV_XS; use Data::Dumper; my $validator = compile( Integer, Decimal[totalDigits => 8, fractionDigits => 3], String[maxLength => 5], Date->plus_coercions( Integer[totalDigits => 8], q{ substr($_, 0, 4)."-".substr($_, 4, 2)."-".substr($_, 6, 2) +} ), String[maxLength => 8], Decimal[totalDigits => 17, fractionDigits => 3], ); my $csv = 'Text::CSV_XS'->new({ sep_char => '|' }); while (my $row = $csv->getline(\*DATA)) { my @fields = $validator->(@$row); print Dumper \@fields; } __DATA__ 12|11.00|BILL|20130131|asd123q|1234.45 14|12.0|MONKEY|20120228|gkhkg|1.2

Produces the following output:

$VAR1 = [ '12', '11.00', 'BILL', '2013-01-01', 'asd123q', '1234.45' ]; Value "MONKEY" did not pass type constraint "String[maxLength=>"5"]" ( +in $_[2]) at validate-csv.pl line 21.

If you've got big files, then you're unlikely to find a faster solution than pairing Text::CSV_XS and Type::Params.

package Cow { use Moo; has name => (is => 'lazy', default => sub { 'Mooington' }) } say Cow->new->name

In reply to Re: Efficient way to do field validation by tobyink
in thread Efficient way to do field validation by govindkailas

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.