On Fri, 22 May 2020 12:52:25 -0400, "Adam Hopkins via RT" <bug-Text-CSV_XS@rt.cpan.org> wrote:
Show quoted text> Actually, I'm not sure that this works? I have this:
>
> my $rows = csv(
> in => $fh,
> headers => 'auto',
> encoding => ':encoding(utf-8)',
> auto_diag => 1,
> callbacks => {
> error => sub {
> my ($err) = @_;
> say "I'm IN HERE $err";
> if ($err == 1012) {
> say "Setting diag to 0";
> Text::CSV_XS->SetDiag(0);
> }
>
> return;
> },
> },
> );
>
> I can see that the code does get in there, but I still get this error
> and my script stops:
>
> INI - the header contains an empty field at /path/to/script.pl
This is because the "header" method calls "croak", as on these errors,
subsequent behavior is undefined and moving on is more dangerous than
trying to continue. Ignoring this error is most likely to cause fail
later on.
What you want for the header is a munger, as described in the manual,
for "db", that would be something like:
--8<---
use Data::Peek;
use Text::CSV_XS qw( csv );
my $no_col = "nc000";
my $rows = csv (
in => *DATA,
encoding => ":encoding(utf-8)",
bom => 1,
munge => sub { lc (s/\W+/_/gr =~ s/^_+//r) || $no_col++ },
auto_diag => 1,
callbacks => {
error => sub {
my ($err) = @_;
say "I'm IN HERE $err";
if ($err == 1012) {
say "Setting diag to 0";
Text::CSV_XS->SetDiag (0);
}
return;
},
},
);
DDumper $rows;
-->8---
=>
I'm IN HERE 2012
[
{ a => '1',
b => '2',
c => '3',
nc000 => ''
}
]
--
H.Merijn Brand
http://tux.nl Perl Monger
http://amsterdam.pm.org/
using perl5.00307 .. 5.31 porting perl5 on HP-UX, AIX, and Linux
https://useplaintext.email https://tux.nl http://www.test-smoke.org
http://qa.perl.org http://www.goldmark.org/jeff/stupid-disclaimers/