Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

Re: lightweight CSV parser

by Tux (Canon)
on Dec 22, 2011 at 09:38 UTC ( [id://944745]=note: print w/replies, xml ) Need Help??


in reply to lightweight CSV parser

As Anonymous Monk already said, parsing from strings is supported from the early days, not only by the PerlIO layer (file handles on scalars are natively supported) but also directly from strings.:

my $csv = Text::CSV_XS->new ({ binary => 1, auto_diag => 1 }); # Using ScalarIO open my $fh, "<", \$csv_stream; while (my $row = $csv->getline ($fh)) { # just like a file, but now on a string ... } close $fh; # Using strings directly. Much less reliable! foreach my $line (@csv_strings) { my @row = $csv->parse ($line); }

Don't try to rewrite a CSV parser. The de-facto parsers Text::CSV_XS and Text::CSV have been tested by millions and already have dozens of options and features as requested by the community.


Enjoy, Have FUN! H.Merijn

Replies are listed 'Best First'.
Re^2: lightweight CSV parser
by wrinkles (Pilgrim) on Dec 22, 2011 at 16:22 UTC
    It works! I've spent the last several days being haunted by The Ghosts of "Don't Write Your Own Crappy Parsers" past, present, and future. There's still time! You, there lad, yes you, go fetch me a Text::CSV module. Merry Christmas!
Re^2: lightweight CSV parser
by wrinkles (Pilgrim) on Dec 22, 2011 at 20:00 UTC
    Thanks Tux, I converted my split functions to use Text::CSV. Here's the new and much-improved code:
    sub _moredata_array { my ($datastring, $datasep_cfg) = @_; use Text::CSV; my $csv = Text::CSV->new ({ binary => 1, auto_diag => 1, sep_char = +> "$datasep_cfg" }); my $io; open ($io, "<:encoding(utf8)", \$datastring) or die "Cannot use CSV: + $!".Text::CSV->error_diag (); my $row = $csv->getline ($io); $row || undef; } sub _moredata_hash { my ($datastring, $hashsep) = @_; use Text::CSV; my $csv = Text::CSV->new ({ binary => 1, auto_diag => 1, sep_char = +> "$hashsep" }); my $io; open ($io, "<:encoding(utf8)", \$datastring) or die "Cannot use CSV: + $!".Text::CSV->error_diag (); my %hash; while (my $colref = $csv->getline($io)) { my $count = scalar @{$colref}; die "$count is an odd number of keys and values at @{$colref}.\n" +if ($count%2); $hash{$colref->[0]} = $colref->[1]; } scalar %hash ? \%hash : undef; }

      As you asked for generic comment in your OP, a few remarks

      # modules are usually not "use"d inside subs, but just once at the cod +e start use Text::CSV; sub _moredata_array { my ($datastring, $datasep_cfg) = @_; # Do not/never quote variables if you do not explicitly want to fo +rce PV context # e.g. if $datasep_cfg is undefined is means something completely +different then # the empty string which it is forced to when quoted my $csv = Text::CSV->new ({ binary => 1, auto_diag => 1, sep_char +=> $datasep_cfg }); # you can declare $io as lexical (my) in the open call itself # At this point,Text::CSV->error_diag () is undefined, as $csv has + not even been used # and opening of the scalar as file handle does not interact with +$csv (yet) open my $io, "<:encoding(utf8)", \$datastring or die "Cannot use C +SV: $!"; # no need to declare a variable # the return value can possible be a reference to an empty list. t +hink if you want # that. || undef is useless here return $csv->getline ($io); } sub _moredata_hash { my ($datastring, $hashsep) = @_; my $csv = Text::CSV->new ({ binary => 1, auto_diag => 1, sep_char +=> $hashsep }); open my $io, "<:encoding(utf8)", \$datastring or die "Cannot use C +SV: $!"; my %hash; while (my $row = $csv->getline($io)) { my $count = scalar @{$row}; $count % 2 and die "$count is an odd number of keys and values + at (@{$row}).\n"; $hash{$row->[0]} = $row->[1]; } scalar %hash ? \%hash : undef; }

      Enjoy, Have FUN! H.Merijn

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others cooling their heels in the Monastery: (6)
As of 2024-04-23 18:51 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found