AnyData2

 view release on metacpan or  search on metacpan

lib/AnyData2/Format/CSV.pm  view on Meta::CPAN

package AnyData2::Format::CSV;

use 5.008001;
use strict;
use warnings FATAL => 'all';

use base qw(AnyData2::Format AnyData2::Role::GuessImplementation);

use Carp 'croak';

=head1 NAME

AnyData2::Format::CSV - CSV format class for AnyData2

=cut

our $VERSION = '0.002';

=head1 METHODS

=head2 new

  my $af = AnyData2->new(
    CSV              => {},
    "File::Linewise" => { filename => File::Spec->catfile( $test_dir, "simple.csv" ) }
  );

constructs a CSV accessor, passes all options down to C<csv_class> beside
C<csv_class>, C<csv_cols> and C<csv_skip_first_row>. C<csv_class> is used
to instantiate the parser and prefers L<Text::CSV_XS> over L<Text::CSV>
by default.  When C<csv_skip_first_row> is set to a true value, the first
line of the csv isn't used to guess the names in C<csv_cols>. Specifying
C<csv_cols> always wins over any value of C<csv_skip_first_row>.

=cut

sub new
{
    my ( $class, $storage, %options ) = @_;
    my $self = $class->SUPER::new($storage);

    my $csv_class          = delete $options{csv_class};
    my $csv_skip_first_row = delete $options{csv_skip_first_row};

    defined $csv_class or $csv_class = $class->_guess_suitable_class(qw(Text::CSV_XS Text::CSV));

    my $csv = $csv_class->new( {%options} );
    $self->{csv} = $csv;

    # XXX
    $self->cols unless ( defined $csv_skip_first_row and $csv_skip_first_row );

    $self;
}

sub _handle_error
{
    my ( $self, $code, $str, $pos, $rec, $fld ) = @_;
    defined $pos and defined $rec and defined $fld and croak "record $rec at line $pos in $fld - $code - $str";
    defined $pos and defined $rec and croak "record $rec at line $pos - $code - $str";
    croak "$code - $str";
}

=head2 cols

Deliver the columns of the CSV ...

=cut

sub cols
{
    my $self = shift;
    defined $self->{csv_cols} and return $self->{csv_cols};
    $self->{csv_cols} = $self->fetchrow;
}

=head2 fetchrow

Parses a line read from storage and return the result

=cut

sub fetchrow
{
    my $self = shift;
    my $buf  = $self->{storage}->read();
    defined $buf or return;
    my $stat = $self->{csv}->parse($buf);
    $stat or return $self->_handle_error( $self->{csv}->error_diag );
    [ $self->{csv}->fields ];
}

=head2 pushrow

Encodes values and write to storage

=cut

sub pushrow
{



( run in 2.587 seconds using v1.01-cache-2.11-cpan-0bb4e1dffa6 )