1 package WebPAC::Input::CSV;
7 use base qw/WebPAC::Common/;
11 use Data::Dump qw/dump/;
15 WebPAC::Input::CSV - support for CSV Export Format
19 our $VERSION = '0.02';
25 Returns new low-level input API object
27 my $input = new WebPAC::Input::CSV(
28 path => '/path/to/records.csv',
41 Default encoding of input file is C<utf-8>
52 my $log = $self->_get_logger();
54 open( my $fh, '<:raw', $arg->{path} ) || $log->logconfess("can't open $arg->{path}: $!");
56 my $csv = Text::CSV->new({ binary => 1 });
61 my $line = $csv->getline( $fh );
64 $log->logdie( "can't parse CSV file ", $csv->error_diag ) unless $line;
67 $rec->{'000'} = [ ++$self->{size} ];
70 foreach my $cell ( @$line ) {
71 my $str = eval { Encode::decode_utf8( $cell ) };
73 if ( $@ =~ m/Cannot decode string with wide characters/ ) {
76 die "ERROR: $@ in line ",dump( $line );
79 $rec->{ $col++ } = $str;
82 push @{ $self->{_rec} }, $rec;
86 $log->debug("loaded ", $self->size, " records");
88 $self ? return $self : return undef;
93 Return record with ID C<$mfn> from database
95 my $rec = $input->fetch_rec( $mfn, $filter_coderef );
100 my ( $self, $mfn, $filter_coderef ) = @_;
102 return $self->{_rec}->[$mfn-1];
108 Return number of records in database
110 my $size = $input->size;
116 return $self->{size};
121 Dobrica Pavlinusic, C<< <dpavlin@rot13.org> >>
123 =head1 COPYRIGHT & LICENSE
125 Copyright 2009 Dobrica Pavlinusic, All Rights Reserved.
127 This program is free software; you can redistribute it and/or modify it
128 under the same terms as Perl itself.
132 1; # End of WebPAC::Input::CSV