Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

Re^3: lightweight CSV parser

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


in reply to Re^2: lightweight CSV parser
in thread lightweight CSV parser

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://944892]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others browsing the Monastery: (3)
As of 2024-03-19 04:34 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found