Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

Text::CSV::Slurp losing in getline_hr

by throop (Chaplain)
on Apr 10, 2012 at 17:13 UTC ( [id://964366]=perlquestion: print w/replies, xml ) Need Help??

throop has asked for the wisdom of the Perl Monks concerning the following question:

I'm trying to read in a CSV data file using Text::CSV::Slurp. It's not working, and my code is so close to the code in Slurp's own example, that I suspect I might have stumbled into some odd configuration problem. Ideas?

In the following test file:

use strict; use Text::CSV::Slurp; use vars qw($file $data); $file = "/orion-reqs/SaferHtml/CradleCSV/2012_03_30/Doc_Sections_SAFER +_Group_Trace.csv"; $data = Text::CSV::Slurp->load(file => $file); 1
$data is an empty array. Going into the debugger, I find that the file is opened correctly, and the header is read. On the first line of data, Slurp calls
$csv->getline_hr($io)
Single step shows that this goes into Text::CSV::AUTOLOAD. That routine expects a method-name that starts with an underscore, which getline_hr doesn't.
my $attr = $Text::CSV::AUTOLOAD; # bound to 'Text::CSV::getline_ +hr' $attr =~ s/.*:://; # Now it's just 'getline_hr' return unless $attr =~ /^_/; # returns with no value
FWIW, my %INC
DB<11> x \%INC 0 HASH(0x98c4780) 'AutoLoader.pm' => '/usr/share/perl5/AutoLoader.pm' 'Carp.pm' => '/usr/share/perl5/Carp.pm' 'Config.pm' => '/usr/lib/perl5/Config.pm' 'Config_git.pl' => '/usr/lib/perl5/Config_git.pl' 'Config_heavy.pl' => '/usr/lib/perl5/Config_heavy.pl' 'DynaLoader.pm' => '/usr/lib/perl5/DynaLoader.pm' 'Exporter.pm' => '/usr/share/perl5/Exporter.pm' 'IO.pm' => '/usr/lib/perl5/IO.pm' 'IO/Handle.pm' => '/usr/lib/perl5/IO/Handle.pm' 'SelectSaver.pm' => '/usr/share/perl5/SelectSaver.pm' 'Symbol.pm' => '/usr/share/perl5/Symbol.pm' 'Term/Cap.pm' => '/usr/share/perl5/Term/Cap.pm' 'Term/ReadLine.pm' => '/usr/share/perl5/Term/ReadLine.pm' 'Text/CSV.pm' => '/home/dthroop/lib/perl/Text/CSV.pm' 'Text/CSV/Slurp.pm' => '/usr/local/share/perl5/Text/CSV/Slurp.pm' 'Text/CSV_XS.pm' => '/usr/lib/perl5/Text/CSV_XS.pm' 'XSLoader.pm' => '/usr/share/perl5/XSLoader.pm' 'dumpvar.pl' => '/usr/share/perl5/dumpvar.pl' 'perl5db.pl' => '/usr/share/perl5/perl5db.pl' 'strict.pm' => '/usr/share/perl5/strict.pm' 'vars.pm' => '/usr/share/perl5/vars.pm' 'warnings.pm' => '/usr/share/perl5/warnings.pm' 'warnings/register.pm' => '/usr/share/perl5/warnings/register.pm'
and what the args to the AUTOLOAD look like:
DB<16> x @_ 0 Text::CSV=HASH(0x8ee2f90) '_AHEAD' => undef '_BOUND_COLUMNS' => undef '_CACHE' => "\"\",\cA\c@\c@\c@\c@\c@\c@\c@\c@\c@\c@\c@\c@\c@\c@\c@\ +c@\c@\c@\c@\c@\c@\cA\c@\c@\c@\c@\c@\cA" '_COLUMN_NAMES' => undef '_EOF' => '' '_FFLAGS' => undef '_FIELDS' => undef '_MODULE' => 'Text::CSV_XS' '_STATUS' => undef '_STRING' => undef 'allow_loose_escapes' => 0 'allow_loose_quotes' => 0 'allow_whitespace' => 0 'always_quote' => 0 'auto_diag' => 0 'binary' => 1 'blank_is_undef' => 0 'empty_is_undef' => 0 'eol' => '' 'escape_char' => '"' 'keep_meta_info' => 0 'quote_char' => '"' 'quote_null' => 1 'quote_space' => 1 'sep_char' => ',' 'types' => undef 'verbatim' => 0 1 IO::Handle=GLOB(0x8cb42e8) -> *Symbol::GEN0 FileHandle({*Symbol::GEN0}) => fileno(6)
Does something look fishy?

Replies are listed 'Best First'.
Re: Text::CSV::Slurp losing in getline_hr
by Tux (Canon) on Apr 11, 2012 at 06:29 UTC

    FWIW Text::CSV_XS (and Text::CSV - which uses Text::CSV_XS if installed - used in Text::CSV::Slurp), have a native method that comes very close to what you are using: getline_hr_all since version 0.80 (2010-12-24).

    Note that the documentation of Text::CSV::Slurp does not mention that a header line is required in the CSV file. Your statement "On the first line of data, Slurp calls $csv->getline_hr ($io)" is not true. It does so for the second line. The first line is read with if (my $head = $csv->getline ($io)) { $csv->column_names ($head); ...

    In Text::CSV_XS speak, your code would be something like:

    use strict; use warnings; use Text::CSV_XS; my $file = "/orion-reqs/SaferHtml/CradleCSV/2012_03_30/Doc_Sections_SA +FER_Group_Trace.csv"; open my $fh, "<:encoding(utf-8)", $file or die "$file: $!"; my $csv = Text::CSV_XS->new ({ binary => 1, auto_diag => 1 }); $csv->column_names ($csv->getline ($fh)); my $data = $csv->getline_hr_all ($fh); close ($fh);

    Enjoy, Have FUN! H.Merijn
    /b
Re: Text::CSV::Slurp losing in getline_hr
by Khen1950fx (Canon) on Apr 10, 2012 at 19:56 UTC
    I can't replicate your problem, but I tried it a little differently. First, don't use use vars. That creates package-scoped global variables which are more trouble than their worth. Just keep it simple.
    #!/usr/bin/perl use strict; use warnings; use Text::CSV::Slurp; use Data::Dumper::Concise; my $file = '/orion-regs/SaferHtml/CradleCSV/2012_03_30/ Doc_Sections/SAFER_Group_Trace.csv'; my $data = Text::CSV::Slurp->load( file => $file, binary => 1 ); print Dumper($data);

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://964366]
Approved by ww
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others studying the Monastery: (8)
As of 2024-03-28 11:58 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found