Subject: | 'parse' method accepts incorrect arguments |
Date: | Wed, 11 Jan 2017 22:10:47 +0000 |
To: | "bug-Text-CSV_XS [...] rt.cpan.org" <bug-Text-CSV_XS [...] rt.cpan.org> |
From: | Charles Stuart Lemmen <CLemmen [...] excelsiorintegrated.com> |
Hiya,
First some info:
Distro: Text::CSV_XS - $VERSION = "1.26";
Perl: This is perl 5, version 16, subversion 3 (v5.16.3) built for MSWin32-x64-multi-thread
O/S:
Caption OperatingSystemSKU OSProductSuite Version
Microsoft Windows 10 Home 101 768 10.0.14393
Next, the issue:
I was using Perl CPAN package "Text::CSV_XS" recently and noticed something odd. I accidentally fed the 'parse' method the results from a valid 'get_line' method call. 'parse' wants a string but I gave it an array ref. What it gave back was an array with one index who's value was the stringified version of the array ref argument:
'ARRAY(0x9e3760)'
No error, no complaints! I had to do some head scratching before I figured out what was going on. I haven't tried other tests such as feeding 'parse' a hash ref but it makes me wonder if, for simplicity sake, 'parse' just stringifies whatever is given to it. I think better would be if it used a line such as the below, which would at least eliminate the caller accidentally giving it a ref:
ref $str and croak 'Argument is a reference not a string!';
I realize it's really up to the caller to not do Bad Things in this case but the above is some pretty non-intrusive argument checking. This could conceivably be considered more a feature request than bug but I'm leaning towards the latter.
Here's the code I used:
use strict;
use warnings;
use Text::CSV_XS;
use Data::Dumper;
my $csv_file_with_header = 'test_csv_xs.csv';
my $csv = Text::CSV_XS->new({binary => 1, auto_diag => 1, eol => $/});
my $csv_fh;
if(open($csv_fh, "<", $csv_file_with_header)) {
my $header_rec = $csv->getline($csv_fh) or die $csv->error_diag();
$csv->parse($header_rec) or die $csv->error_diag();
my @columns = $csv->fields();
print Dumper(\@columns) . "\n";
close $csv_fh;
}
exit 1;
And here is what the header record looks like:
"Entry Type","Cust ID",Company,"Item ID","Item Ident","Kit ID","Kit Ident","Date Sync",Notes
And here's the result:
$VAR1 = [
'ARRAY(0x9e3760)'
];
Thanks for writing and maintaining Text::CSV_XS :)
Stuart Lemmen
IT Development & Support
Excelsior Integrated LLC
413-394-4340
clemmen@excelsiorintegrated.com<mailto:clemmen@excelsiorintegrated.com>
www.excelsiorintegrated.com<http://www.excelsiorintegrated.com/>
[Excelsior Integrated Small][MCM 3PL seal vector]