#!/usr/bin/perl -T use strict; use warnings; use Data::Dumper; use Text::CSV_XS; use HTML::Template; use CGI qw(header param textarea); my (@parsed,@error); my $tmpl = HTML::Template->new(filehandle => \*DATA); $tmpl->param(textarea => textarea('csv',undef,8,30)); my $i = 1; if (param('go')) { my $csv = Text::CSV_XS->new; for (split(/\r?\n/, param('csv'))) { if ($csv->parse($_)) { my @field = $csv->fields; if (@field < 4) { my $missing = 4 - @field; push @error, {line=>$i, error=>"$missing fields missing"}; push @parsed, undef; } else { push @parsed, [$csv->fields]; } } else { push @error, {line=>$i, error=>$csv->error_input}; push @parsed, undef; } $i += 1; } $tmpl->param(dump => Dumper(\@parsed), errors => \@error); } print header,$tmpl->output; __DATA__
Here is what i was able to parse: