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?
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.