/[webpac2]/trunk/lib/WebPAC/Input/CSV.pm
This is repository of my old source code which isn't updated any more. Go to git.rot13.org for current projects!
ViewVC logotype

Annotation of /trunk/lib/WebPAC/Input/CSV.pm

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1254 - (hide annotations)
Mon Jul 27 16:24:41 2009 UTC (14 years, 10 months ago) by dpavlin
File size: 1859 byte(s)
force utf-8 encoding on all data comming from file
1 dpavlin 1186 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 dpavlin 1254 use Encode;
11 dpavlin 1186 use Data::Dump qw/dump/;
12    
13     =head1 NAME
14    
15     WebPAC::Input::CSV - support for CSV Export Format
16    
17     =cut
18    
19 dpavlin 1254 our $VERSION = '0.02';
20 dpavlin 1186
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 dpavlin 1254 open( my $fh, '<:raw', $arg->{path} ) || $log->logconfess("can't open $arg->{path}: $!");
55 dpavlin 1186
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 dpavlin 1254 $rec->{ $col++ } = Encode::decode_utf8( $_ ) foreach @$line;
71 dpavlin 1186
72     push @{ $self->{_rec} }, $rec;
73    
74     };
75    
76     $log->debug("loaded ", $self->size, " records");
77    
78     $self ? return $self : return undef;
79     }
80    
81     =head2 fetch_rec
82    
83     Return record with ID C<$mfn> from database
84    
85     my $rec = $input->fetch_rec( $mfn, $filter_coderef );
86    
87     =cut
88    
89     sub fetch_rec {
90 dpavlin 1254 my ( $self, $mfn, $filter_coderef ) = @_;
91 dpavlin 1186
92     return $self->{_rec}->[$mfn-1];
93     }
94    
95    
96     =head2 size
97    
98     Return number of records in database
99    
100     my $size = $input->size;
101    
102     =cut
103    
104     sub size {
105     my $self = shift;
106     return $self->{size};
107     }
108    
109     =head1 AUTHOR
110    
111     Dobrica Pavlinusic, C<< <dpavlin@rot13.org> >>
112    
113     =head1 COPYRIGHT & LICENSE
114    
115     Copyright 2009 Dobrica Pavlinusic, All Rights Reserved.
116    
117     This program is free software; you can redistribute it and/or modify it
118     under the same terms as Perl itself.
119    
120     =cut
121    
122     1; # End of WebPAC::Input::CSV

  ViewVC Help
Powered by ViewVC 1.1.26