Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

Re: Efficient way to do field validation

by tobyink (Canon)
on Jul 31, 2013 at 12:56 UTC ( [id://1047256]=note: print w/replies, xml ) Need Help??


in reply to Efficient way to do field validation

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

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1047256]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others lurking in the Monastery: (4)
As of 2024-04-19 12:04 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found