I have a script that fails with errors when I run it outside the debugger but does not fail (no errors) when in the debugger. This is annoying as I do not know how to fix it.
#!/usr/bin/perl
use Text::CSV_XS;
use IO::File;
my $argcount = 0;
my $infile = "";
my $arg;
my %email;
my $em;
while (@ARGV) {
$arg = shift @ARGV;
if ($argcount eq 0) {
$argcount++;
$infile = $arg;
print "Processing: $infile\n";
}
}
my @rows;
my $lineterm = "\r\n";
my $csv = Text::CSV_XS->new ( { eol => $lineterm, binary => 1 } ) # s
+hould set binary attribute.
or die "Cannot use CSV: ".Text::CSV->error_diag ();
open my $fh, "<:encoding(utf8)", $infile or die "$infile: $!";
while ( my $row = $csv->getline( $fh ) ) {
# $row->[2] =~ m/pattern/ or next; # 3rd field should match
$em = lc($row->[22]);
if ($em eq "0") {
#print "BAD: ".join(":",@$row)."\n";
}
#print "Email: $em\n";
$email{$em}++;
if ($email{$em} eq 1) {
#print "New email: $em\n";
$row->[22] = $em;
} elsif ($email{$em} > 1) {
$row->[22] = "";
}
push @rows, $row;
}
$csv->eof or $csv->error_diag();
close $fh;
open $fh, ">:encoding(utf8)", "out2.csv" or die "out2.csv: $!";
foreach $row (@rows) {
my @columns = $csv->fields($row);
my $status = $csv->combine($row) or warn($csv->error_input());
my $outstr = $csv->string();
print $fh "$outstr\n";
}
close $fh;
I am away from that development server but the error was something like unable to load method in IO::Handle.
Can anyone help enlighten this puzzled monk on why this might be?
Update:
Seems like it never fails. I finally got back to my development box and tried this again to produce the errors and pull some sample input, and lo, the errors are now gone. I do not know why this was not working before but I have been working to clean up the input data.
The error was on the while look where I am calling $csv->getline($fh). Would the contents of the line cause an error with the IO stream and cause IO::File to object to the getline method?