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
|