Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

Re: CSV EOL Question

by Tux (Monsignor)
on Jan 22, 2014 at 15:17 UTC ( #1071633=note: print w/ replies, xml ) Need Help??


in reply to CSV EOL Question

The default for the eol attribute is undefined, so the parser can parse records ending on any valid line ending: \r, \r\n, and \n. To produce records, the CSV object should know to do line endings, hence you need to set eol.

It is way to late now to change the default to be sensible and use $/ on print, which would have been a fine choice in the beginning.

As print takes a reference, why not simplify the code and make it faster?:

my $csv = Text::CSV->new ({ binary => 1, auto_diag => 1, eol => $/ }); open my $fh, ">:encoding(utf8)", $filename or die "$filename: $!"; $sth = $dbh->prepare ($query) or # No need to put "'s around $query error ($q, "Problem with database call"); if ($sth->execute) { # not using RaiseError? while (my $ref = $sth->fetch) { # fetching data by ref is faster $csv->print ($fh, $ref); } }

Enjoy, Have FUN! H.Merijn


Comment on Re: CSV EOL Question
Select or Download Code
Re^2: CSV EOL Question
by jrod16 (Initiate) on Jan 22, 2014 at 18:13 UTC
    Thanks for the insight everyone! Tux, I attempted using your code with the scalar as a reference but the csv command is specifically looking for an array reference. Am I doing something wrong?
    if($sth->execute) { while(my $dat = $sth->fetchrow) {$csv->print($fh, +$dat); }
    Expected fields to be an array ref at ./name.pl line 65. I feel as though this will only work as \@dat.

      Yes, fetchrow returns a list, fetch is an alias to fetchrow_arrayref and returns a reference.


      Enjoy, Have FUN! H.Merijn

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1071633]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others meditating upon the Monastery: (6)
As of 2014-08-30 22:34 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The best computer themed movie is:











    Results (294 votes), past polls