Dmitriy Dontsov > Snail-CSV-0.07 > Snail::CSV

Download:
Snail-CSV-0.07.tar.gz

Dependencies

Annotate this POD

View/Report Bugs
Module Version: 0.07   Source  

NAME ^

Snail::CSV - Perl extension for read/write/update CSV files.

SYNOPSIS ^

  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

DESCRIPTION ^

This module can be used to read/write/update data from/to CSV files. Text::CSV_XS is used for parsing CSV files.

METHOD ^

new()
new(\%args)

This is constructor. %args - Text::CSV_XS options. Return object.

setFile('file.csv', \@fields_name)
setFile('file.csv', \@fields_name, \%filter)

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; }
               );
parse

Read and parse CSV file. Return arrayref.

fetchall_arrayref

An alternative to parse. Return arrayref.

fetchall_hashref

An alternative to parse. Return hashref.

getData

Return current data. Use this method after parse (fetchall_arrayref, fetchall_hashref).

setData(\@data)
setData(\%data)

Set new data. Return object.

update
update('/full/path/to/new_file.csv')

Attention! If new file not defined, update current file. Return object.

save
save('/full/path/to/new_file.csv')

Save current object data. Attention! If new file not defined, save data to current file. Return object.

version

Return version number.

EXPORT

None by default.

EXAMPLE ^

First example.

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:

  $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'
            }
          ];

Other example.

        Done.

TODO ^

Goog idea? Welcome...

SEE ALSO ^

Text::CSV_XS, IO::File

AUTHOR ^

Dmitriy Dontsov, <mit@cpan.org>

COPYRIGHT AND LICENSE ^

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.

syntax highlighting: