NAME
Mojo::CSV - no-nonsense CSV handling
SYNOPSIS
# Just convert data to CSV and gimme it
say Mojo::CSV->new->text([
[ qw/foo bar baz/ ],
[ qw/moo mar mer/ ],
]);
my $data = Mojo::CSV->new->slurp('file.csv') # returns a Mojo::Collection
->grep(sub{ $_->[0] eq 'Zoffix' });
my $csv = Mojo::CSV->new( in => 'file.csv' );
while ( my $row = $csv->row ) {
# Read row-by-row
}
# Write CSV file in one go
Mojo::CSV->new->spurt( $data => 'file.csv' );
# Write CSV file row-by-row
my csv = Mojo::CSV->new( out => 'file.csv' );
$csv->trickle( $row ) for @data;
DESCRIPTION
Read and write CSV (Comma Separated Values) like a boss, Mojo style.
METHODS
Unless otherwise indicated, all methods return their invocant.
new
Mojo::CSV->new;
Mojo::CSV->new( in => 'file.csv', out => 'file.csv' );
Creates a new Mojo::CSV
object. Takes two optional arguments. See "in" and "out" for details.
flush
$csv->flush;
Closes "out" filehandle. Call this when you're done spurting or trickling data. This will be done automatically when the Mojo::CSV
object is destroyed.
in
my $csv = Mojo::CSV->new( in => 'file.csv' );
$csv->in('file.csv');
open my $fh, '<', 'foo.csv' or die $!;
$csv->in( $fh );
$csv->in( Mojo::Asset::File->new(path => 'file.csv') );
$csv->in( Mojo::Asset::Memory->new->add_chunk('foo,bar,baz') );
Specifies the input for "slurp" and "row". Takes a filename, an opened filehandle, a Mojo::Asset::File object, or a Mojo::Asset::Memory object.
out
my $csv = Mojo::CSV->new( out => 'file.csv' );
$csv->out('file.csv');
open my $fh, '>', 'foo.csv' or die $!;
$csv->out( $fh );
Specifies the output for "spurt" and "trickle". Takes either a filename or an opened filehandle.
row
my $row = $csv->row;
Returns an arrayref, which is a row of data from the CSV. The thing to read must be first set with in method or in
argument to "new"
slurp
my $data = Mojo::CSV->new->slurp('file.csv');
my $data = Mojo::CSV->new( in => 'file.csv' )->slurp;
Returns a Mojo::Collection object, each item of which is an arrayref representing a row of CSV data.
slurp_body
my $data = Mojo::CSV->new->slurp_body('file.csv');
A shortcut for calling "slurp" and discarding the first row (use this for CSV with headers you want to get rid of).
spurt
Mojo::CSV->new->spurt( $data => 'file.csv');
Mojo::CSV->new( out => 'file.csv' )->spurt( $data );
Writes a data structure into CSV. $data
is an arrayref of rows, which are themselves arrayrefs (each item is a cell data).
text
say Mojo::CSV->new->text([qw/foo bar baz/]);
say Mojo::CSV->new->text([
[ qw/foo bar baz/ ],
[ qw/moo mar mer/ ],
]);
Returns a CSV string. Takes either a single arrayref to encode just a single row or an arrayref of arrayrefs to include multiple rows. Arrayref-like things should work too.
trickle
my $csv = Mojo::CSV->new( out => 'file.csv' );
$csv->trickle( $_ ) for @data;
Writes out a single row of CSV. Takes an arrayref of cell values.
SEE ALSO
REPOSITORY
Fork this module on GitHub: https://github.com/zoffixznet/Mojo-CSV
BUGS
To report bugs or request features, please use https://github.com/zoffixznet/Mojo-CSV/issues
If you can't access GitHub, you can email your request to bug-Mojo-CSV at rt.cpan.org
AUTHOR
LICENSE
You can use and distribute this module under the same terms as Perl itself. See the LICENSE
file included in this distribution for complete details.