26dab49fda5d42697187803a4261693093d2d5c6
[webpac2] / lib / WebPAC / Input / CSV.pm
1 package WebPAC::Input::CSV;
2
3 use warnings;
4 use strict;
5
6 use WebPAC::Input;
7 use base qw/WebPAC::Common/;
8
9 use Text::CSV;
10 use Encode;
11 use Data::Dump qw/dump/;
12
13 =head1 NAME
14
15 WebPAC::Input::CSV - support for CSV Export Format
16
17 =cut
18
19 our $VERSION = '0.02';
20
21 =head1 FUNCTIONS
22
23 =head2 new
24
25 Returns new low-level input API object
26
27   my $input = new WebPAC::Input::CSV(
28         path => '/path/to/records.csv',
29   );
30
31 Options:
32
33 =over 4
34
35 =item path
36
37 path to CSV file
38
39 =back
40
41 Default encoding of input file is C<utf-8>
42
43 =cut
44
45 sub new {
46         my $class = shift;
47         my $self = {@_};
48         bless($self, $class);
49
50         my $arg = {@_};
51
52         my $log = $self->_get_logger();
53
54         open( my $fh, '<:raw', $arg->{path} ) || $log->logconfess("can't open $arg->{path}: $!");
55
56         my $csv = Text::CSV->new({ binary => 1 });
57
58         $self->{size} = 0;
59
60         while ( 1 ) {
61                 my $line = $csv->getline( $fh );
62                 last if $csv->eof;
63
64                 $log->logdie( "can't parse CSV file ", $csv->error_diag ) unless $line;
65
66                 my $rec;
67                 $rec->{'000'} = [ ++$self->{size} ];
68
69                 my $col = 'A';
70                 foreach my $cell ( @$line ) {
71                         my $str = eval { Encode::decode_utf8( $cell ) };
72                         if ( $@ ) {
73                                 if ( $@ =~ m/Cannot decode string with wide characters/ ) {
74                                         $str = $cell;
75                                 } else {
76                                         die "ERROR: $@ in line ",dump( $line );
77                                 }
78                         } else {
79                                 utf8::upgrade( $cell );
80                                 $str = $cell;
81                         }
82                                 
83                         $rec->{ $col++ } = $str;
84                 }
85
86                 push @{ $self->{_rec} }, $rec;
87
88         };
89
90         $log->debug("loaded ", $self->size, " records");
91
92         $self ? return $self : return undef;
93 }
94
95 =head2 fetch_rec
96
97 Return record with ID C<$mfn> from database
98
99   my $rec = $input->fetch_rec( $mfn, $filter_coderef );
100
101 =cut
102
103 sub fetch_rec {
104         my ( $self, $mfn, $filter_coderef ) = @_;
105
106         return $self->{_rec}->[$mfn-1];
107 }
108
109
110 =head2 size
111
112 Return number of records in database
113
114   my $size = $input->size;
115
116 =cut
117
118 sub size {
119         my $self = shift;
120         return $self->{size};
121 }
122
123 =head1 AUTHOR
124
125 Dobrica Pavlinusic, C<< <dpavlin@rot13.org> >>
126
127 =head1 COPYRIGHT & LICENSE
128
129 Copyright 2009 Dobrica Pavlinusic, All Rights Reserved.
130
131 This program is free software; you can redistribute it and/or modify it
132 under the same terms as Perl itself.
133
134 =cut
135
136 1; # End of WebPAC::Input::CSV