Text-CSV_PP-Simple

 view release on metacpan or  search on metacpan

lib/Text/CSV_PP/Simple.pm  view on Meta::CPAN

package Text::CSV_PP::Simple;

use warnings;
use strict;
use Carp;

use version; our $VERSION = qv('0.0.5');

use Text::CSV_PP;
use IO::File;

sub new {
    my $class = shift;
    return bless { _parser => Text::CSV_PP->new(@_), } => $class;
}

sub field_map {
    my $self = shift;
    if (@_) {
        $self->{_map} = [@_];
    }
    return @{ $self->{_map} || [] };
}

sub want_fields {
    my $self = shift;
    if (@_) {
        $self->{_wanted} = [@_];
    }
    return @{ $self->{_wanted} || [] };
}

sub read_file {
    my ($self, $file) = @_;
    
    my @result;
    my $csv = $self->{"_parser"};
    my $fh = IO::File->new($file, 'r') or croak $!;
    while (not $fh->eof) {
        my $cells = $csv->getline($fh);
        if (my @wanted = $self->want_fields){
            @{$cells} = @{$cells}[@wanted];
        }
        use Data::Dumper;
        print Dumper $cells;
        
        
        my $addition = $cells;
        if (my @map = $self->field_map ){
            my $hash = { map { $_ => shift @{$cells} } @map };
            delete $hash->{null};
            $addition = $hash;
        }
        push @result, $addition;
    }
    return @result;
}

1; 
__END__

=head1 NAME

Text::CSV_PP::Simple - Simpler parsing of CSV files [PP version]

=head1 VERSION

This document describes Text::CSV_PP::Simple version 0.0.5

=head1 SYNOPSIS

    use Text::CSV_PP::Simple;
    my $parser = Text::CSV_PP::Simple->new;
    my @data = $parser->read_file($datafile);

 view all matches for this distribution
 view release on metacpan -  search on metacpan

( run in 1.341 second using v1.00-cache-2.02-grep-82fe00e-cpan-d29e8ade9f55 )