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 utf8::upgrade( $cell );
83 $rec->{ $col++ } = $str;
86 push @{ $self->{_rec} }, $rec;
90 $log->debug("loaded ", $self->size, " records");
92 $self ? return $self : return undef;
97 Return record with ID C<$mfn> from database
99 my $rec = $input->fetch_rec( $mfn, $filter_coderef );
104 my ( $self, $mfn, $filter_coderef ) = @_;
106 return $self->{_rec}->[$mfn-1];
112 Return number of records in database
114 my $size = $input->size;
120 return $self->{size};
125 Dobrica Pavlinusic, C<< <dpavlin@rot13.org> >>
127 =head1 COPYRIGHT & LICENSE
129 Copyright 2009 Dobrica Pavlinusic, All Rights Reserved.
131 This program is free software; you can redistribute it and/or modify it
132 under the same terms as Perl itself.
136 1; # End of WebPAC::Input::CSV