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',
42 Default encoding of input file is C<utf-8>
44 C<header_first> will use first line as header names.
55 my $log = $self->_get_logger();
57 open( my $fh, '<:raw', $arg->{path} ) || $log->logconfess("can't open $arg->{path}: $!");
59 my $csv = Text::CSV->new({ binary => 1 });
63 if ( $self->{header_first} ) {
64 my $line = $csv->getline( $fh );
65 $self->{header_names} = $line;
66 $self->debug( "header_names = ",dump( $self->{header_names} ) );
70 my $line = $csv->getline( $fh );
73 $log->logdie( "can't parse CSV file ", $csv->error_diag ) unless $line;
76 $rec->{'000'} = [ ++$self->{size} ];
81 foreach my $cell ( @$line ) {
82 my $str = eval { Encode::decode_utf8( $cell ) };
84 if ( $@ =~ m/Cannot decode string with wide characters/ ) {
87 die "ERROR: $@ in line ",dump( $line );
90 utf8::upgrade( $cell );
94 $rec->{ $col++ } = $str;
96 if ( $self->{header_names} ) {
97 $rec->{ $self->{header_names}->[$header_pos] } = $str;
102 push @{ $self->{_rec} }, $rec;
106 $log->debug("loaded ", $self->size, " records");
108 $self ? return $self : return undef;
113 Return record with ID C<$mfn> from database
115 my $rec = $input->fetch_rec( $mfn, $filter_coderef );
120 my ( $self, $mfn, $filter_coderef ) = @_;
122 return $self->{_rec}->[$mfn-1];
128 Return number of records in database
130 my $size = $input->size;
136 return $self->{size};
141 Dobrica Pavlinusic, C<< <dpavlin@rot13.org> >>
143 =head1 COPYRIGHT & LICENSE
145 Copyright 2009 Dobrica Pavlinusic, All Rights Reserved.
147 This program is free software; you can redistribute it and/or modify it
148 under the same terms as Perl itself.
152 1; # End of WebPAC::Input::CSV