Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

TexT::CSV and getline

by BernieC (Pilgrim)
on Nov 04, 2020 at 19:43 UTC ( [id://11123402]=perlquestion: print w/replies, xml ) Need Help??

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

This is very odd so I must be missing something really obvious. My test program:
#!/usr/bin/perl use v5.10 ; use strict; use warnings ; use Text::CSV ; use FileHandle ; my $csv = Text::CSV->new((binary => 1)) ; my $fh = new FileHandle ; open($fh, "<", $ARGV[0]) or die "Can't open $ARGV[0]: $!\n" ; say $fh ; my $row = $csv->getline($fh) ; exit
and when I run it, I get:
Desktop>csvtest.pl testcsv.csv FileHandle=GLOB(0x1e9c20) Can't call method "getline" on an undefined value at D:\Desktop\csvtes +t.pl line 16.
I printed out $fh and it was clearly not undefined. The file is a vanilla windows-format file and neither my text editor nor excel nor word have a problem with it. ??? My little test file was pretty trivial:
Importance,Company,Department,"Job Title"

Replies are listed 'Best First'.
Re: TexT::CSV and getline
by GrandFather (Saint) on Nov 04, 2020 at 20:16 UTC

    The Text::CSV documentation says in part:

    new

    (Class method) Returns a new instance of class Text::CSV. The attributes are described by the (optional) hash ref \%attr

    So your $csv construction line should be:

    my $csv = Text::CSV->new({binary => 1}) ;

    Note the currly braces {} instead of the parenthesis () as the argument to new.

    Optimising for fewest key strokes only makes sense transmitting to Pluto or beyond
      Perfect --- in the docs {and with my bad eyes} I missed that a hashref was needed. THANKS!!
Re: TexT::CSV and getline
by swl (Parson) on Nov 04, 2020 at 21:03 UTC

    GrandFather has answered the question, so this just another data point.

    Text::CSV returns undef when it is passed incorrect arguments. If you had added a print for the $csv as well as $fh then this would have come up as a warning about printing an undefined value. Or you could have added:

    die "CSV object is undefined" if !defined $csv;

    This is one of those things that is much clearer in hindsight.

Re: TexT::CSV and getline
by BillKSmith (Monsignor) on Nov 04, 2020 at 21:43 UTC
    Fix your constructor as GrandFather indicated, and your code 'works'. You probably did not intend to output a reference to a filehandle. I have made your test case the default input, and added one line to print $row.
    BEGIN{ $ARGV[0] //= \qq(Importance,Company,Department,"Job Title"\n); } use v5.10 ; use strict; use warnings ; use Text::CSV ; use FileHandle ; my $csv = Text::CSV->new({binary => 1}) ; my $fh = new FileHandle ; open( $fh, "<", $ARGV[0]) or die "Can't open $ARGV[0]: $!\n" ; say $fh ; my $row = $csv->getline($fh) ; say do{ local $" = "\n"; "@$row" }; exit

    OUTPUT

    FileHandle=GLOB(0x8bba28) Importance Company Department Job Title

    You do not need both FileHandle and open. Either one will do the job.

    Bill
Re: TexT::CSV and getline
by jszinger (Scribe) on Nov 04, 2020 at 20:02 UTC
    Can't call method "getline" on an undefined value at D:\Desktop\csvtes +t.pl line 16.
    says to me that $csv is undefined.
Re: Text::CSV and getline
by davies (Prior) on Nov 04, 2020 at 20:07 UTC

    I've never used the Filehandle module, so I'm guessing, but you don't appear to be using it as specified in the docs. The first thing I would try is to use a normal file handle without using the module, creating it in the "open" command, so: open(my $fh, "<", $ARGV[0]) etc.

    Regards,

    John Davies

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others about the Monastery: (2)
As of 2024-04-26 07:16 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found