Snail::CSV - Perl extension for read/write/update CSV files.
use Snail::CSV; my $csv = Snail::CSV->new(\%args); # %args - Text::CSV_XS options my %filter = ( 'pq' => 3, 'name' => sub { my $name = shift; $name =~ /XP$/ ? 1 : 0; } ); $csv->setFile("lamps.csv", [ "id", "name", "pq" ], \%filter); my $lamps = $csv->parse; # or $csv->parse; # some code my $lamps = $csv->getData; $csv->setFile("tents.csv", [ "id", "name", "brand", "price" ]); my $tents = $csv->fetchall_hashref; # $tents is HASHREF for my $item (values %{$tents}) { $item->{'price'} = $item->{'brand'} eq 'Marmot' ? 0.95 * $item->{'price'} : $item->{'price'}; } $csv->setData($tents); $csv->update; # to tents.csv # or for my $item ( @{ $csv->fetchall_arrayref } ) { $item->{'price'} = $item->{'brand'} eq 'Marmot' ? 0.95 * $item->{'price'} : $item->{'price'}; } $csv->update("/full/path/to/new_file.csv"); # to new CSV file
This module can be used to read/write/update data from/to CSV files. Text::CSV_XS is used for parsing CSV files.
This is constructor. %args - Text::CSV_XS options. Return object.
Set CSV file, fields name and filters for fields name. Return object.
Fields and Filters:
my @fields_name = ("id", "name", "pq"); my %filter = ( 'pq' => 3, 'name' => sub { my $name = shift; $name =~ /XP$/ ? 1 : 0; } );
Read and parse CSV file. Return arrayref.
An alternative to parse. Return arrayref.
An alternative to parse. Return hashref.
Return current data. Use this method after parse (fetchall_arrayref, fetchall_hashref).
Set new data. Return object.
Attention! If new file not defined, update current file. Return object.
Save current object data. Attention! If new file not defined, save data to current file. Return object.
Return version number.
None by default.
Code:
#!/usr/bin/perl -w use strict; use Snail::CSV; use Data::Dumper; my $csv = Snail::CSV->new(); $csv->setFile("lamps.csv", [ "id", "name", "pq" ]); # or $csv->setFile("lamps.csv", [ "id", "", "pq" ], { 'pq' => sub { my $pq = shift; $pq > 2 ? 1 : 0; } }); my $lamps = $csv->parse; print Dumper($lamps);
lamps.csv
1;"Tikka Plus";3 2;"Myo XP";1 3;"Duobelt Led 8";5
If you wrote:
$csv->setFile("lamps.csv", [ "id", "name", "pq" ]);
then dump is:
dump
$VAR1 = [ { 'id' => '1', 'name' => 'Tikka Plus', 'pq' => '3' }, { 'id' => '2', 'name' => 'Myo XP', 'pq' => '1' }, { 'id' => '3', 'name' => 'Duobelt Led 8', 'pq' => '5' } ];
but if:
$csv->setFile("lamps.csv", [ "id", "", "pq" ], { 'pq' => sub { my $pq = shift; $pq > 2 ? 1 : 0; } });
dump is:
$VAR1 = [ { 'id' => '1', 'pq' => '3' }, { 'id' => '3', 'pq' => '5' } ];
Done.
Goog idea? Welcome...
Text::CSV_XS, IO::File
Dmitriy Dontsov, <mit@cpan.org>
Copyright (C) 2006 by Dmitriy Dontsov
This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself, either Perl version 5.8.8 or, at your option, any later version of Perl 5 you may have available.
To install Snail::CSV, copy and paste the appropriate command in to your terminal.
cpanm
cpanm Snail::CSV
CPAN shell
perl -MCPAN -e shell install Snail::CSV
For more information on module installation, please visit the detailed CPAN module installation guide.