Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

Spreadsheet::Read not reading CSV data passed as string

by 1nickt (Canon)
on Jul 30, 2017 at 04:18 UTC ( [id://1196285]=perlquestion: print w/replies, xml ) Need Help??

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

Greetings learned brethren, and special salutations to Brother Tux, to whom in particular I direct this plea for assistance.

If I read the documentation for Spreadsheet::Read correctly, I should be able to pass a string containing CSV data as the sole or the first argument to ReadData(). While the SYNOPSIS does not show an example, the doc for the function does:

my $book = ReadData ($content);
... and describes the function as "Tries to convert the given file, string, or stream ..." (emphasis added).

But when I try to pass a string containing simple CSV data, I get an error. First, with parser option set as recommended in the doc:

perl -Mstrict -MData::Dumper -MSpreadsheet::Read -wE 'my $data=ReadDat +a("A,B,C\na,b,c",parser=>"csv",debug=>9); say Dumper $data' $Options = { 'rc' => 1, 'attr' => 0, 'dtfmt' => 'yyyy-mm-dd', 'parser' => 'csv', 'cells' => 1, 'clip' => 1, 'strip' => 0, 'debug' => 9 }; Opening CSV IO using Text::CSV_XS-1.26 CSV sep_char ',', quote_char '"' Can't locate object method "getline" via package "A,B,C a,b,c" (perhaps you forgot to load "A,B,C a,b,c"?) at /Users/nick/perl5/perlbrew/perls/perl-5.22.0/lib/site_perl +/5.22.0/Spreadsheet/Read.pm line 517.
Also trying without the parser option:
perl -Mstrict -MData::Dumper -MSpreadsheet::Read -wE 'my $data=ReadDat +a("A,B,C\na,b,c",debug=>9); say Dumper $data' $Options = { 'attr' => 0, 'cells' => 1, 'strip' => 0, 'rc' => 1, 'debug' => 9, 'dtfmt' => 'yyyy-mm-dd', 'clip' => 1 }; $VAR1 = undef;
( ... although, interestingly, when I try that from a file, I get an error, and different from the one obtained when using parser:
$ cat SR.pl use strict; use warnings; use feature 'say'; use Data::Dumper; use Spreadsheet::Read; my $csv = do { local $/; <DATA> }; my $data = ReadData( $csv, debug => 9 ); say Dumper $data; __DATA__ A,B,C a,b,c
perl SR.pl $Options = { 'strip' => 0, 'cells' => 1, 'rc' => 1, 'clip' => 1, 'dtfmt' => 'yyyy-mm-dd', 'attr' => 0, 'debug' => 9 }; Unsuccessful stat on filename containing newline at /Users/nick/perl5/ +perlbrew/perls/perl-5.22.0/lib/site_perl/5.22.0/Spreadsheet/Read.pm l +ine 849, <DATA> line 1. $VAR1 = undef;
)

FWIW I do have:

$ perl -MIO::Scalar -E 'say $IO::Scalar::VERSION' 2.111

Thanks, o wise ones.


The way forward always starts with a minimal test.

Replies are listed 'Best First'.
Re: Spreadsheet::Read not reading CSV data passed as string
by shmem (Chancellor) on Jul 30, 2017 at 08:24 UTC

    Well, no need to pray at line 547 in Spreadsheet::Read .-)
    It should be

    - $in = $txt; # Now pray ... + open $in, '<', \$txt or return;

    since below follows

    while (my $row = $csv->getline ($in)) {

    which doesn't work on a plain string, but on a file handle.

    perl -le'print map{pack c,($-++?1:13)+ord}split//,ESEL'

      That would break perl -MDP -MSpreadsheet::Read -we'$_="a,b,c\n1,2,3\n";open$a,"<",\$_;DDumper ReadData($a,parser=>"csv",debug=>9)'


      Enjoy, Have FUN! H.Merijn
        That would break

        Adding case for a GLOB ref:

        else { if ($io_ref) { # GLOB/IO ref $in = $txt; # Now pray ... } else { # just text open $in, "<", \$txt or return; } }
        perl -le'print map{pack c,($-++?1:13)+ord}split//,ESEL'
Re: Spreadsheet::Read not reading CSV data passed as string
by Tux (Canon) on Jul 30, 2017 at 09:00 UTC

    Thanks for the report. The code that tried this combination had a comment:

    # Now pray!

    Nuf said. Now fixed. Care to try?

    $ wget --output-document=Spreadsheet-Read-git.tgz \ 'https://github.com/Tux/Spreadsheet-Read/archive/master.tar +.gz'

    update 15:00: I just released 0.73 with fixes and tests


    Enjoy, Have FUN! H.Merijn

      Many thanks, Tux! My test scriptlets as well as my real app are working as expected now. Wonderful!


      The way forward always starts with a minimal test.
Re: Spreadsheet::Read not reading CSV data passed as string
by kevbot (Vicar) on Jul 30, 2017 at 05:53 UTC
    It appears that ReadData requires a file handle when using the csv parser. The following seems to work.
    #!/usr/bin/env perl use strict; use warnings; use v5.10; use Data::Dumper; use Spreadsheet::Read; my $data_str = "A,B,C\na,b,c"; open (my $fh, '<', \$data_str ) or die 'Could not open file handle'; my $data = ReadData( $fh, parser=>"csv", debug => 9 ); close($fh); say Dumper $data; exit;

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (3)
As of 2024-03-29 01:55 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found