1 |
package WebPAC::Input::TSV; |
2 |
|
3 |
use warnings; |
4 |
use strict; |
5 |
|
6 |
use WebPAC::Input; |
7 |
use base qw/WebPAC::Common/; |
8 |
|
9 |
use Encode; |
10 |
use Data::Dump qw/dump/; |
11 |
|
12 |
=head1 NAME |
13 |
|
14 |
WebPAC::Input::TSV - tab separated values |
15 |
|
16 |
=cut |
17 |
|
18 |
=head1 FUNCTIONS |
19 |
|
20 |
=head2 new |
21 |
|
22 |
my $input = new WebPAC::Input::TSV( |
23 |
path => '/path/to/records.tsv', |
24 |
); |
25 |
|
26 |
=back |
27 |
|
28 |
Default encoding of input file is C<utf-8> |
29 |
|
30 |
=cut |
31 |
|
32 |
sub new { |
33 |
my $class = shift; |
34 |
my $self = {@_}; |
35 |
bless($self, $class); |
36 |
|
37 |
my $arg = {@_}; |
38 |
|
39 |
my $log = $self->_get_logger(); |
40 |
|
41 |
open( my $fh, '<:raw', $arg->{path} ) || $log->logconfess("can't open $arg->{path}: $!"); |
42 |
|
43 |
$self->{size} = 0; |
44 |
|
45 |
while ( my $line = <$fh> ) { |
46 |
|
47 |
my $rec; |
48 |
$rec->{'000'} = [ ++$self->{size} ]; |
49 |
|
50 |
my $col = 'A'; |
51 |
$rec->{ $col++ } = Encode::decode_utf8( $_ ) foreach split(/\t/,$line); |
52 |
|
53 |
push @{ $self->{_rec} }, $rec; |
54 |
|
55 |
}; |
56 |
|
57 |
$log->debug("loaded ", $self->size, " records"); |
58 |
|
59 |
$self ? return $self : return undef; |
60 |
} |
61 |
|
62 |
=head2 fetch_rec |
63 |
|
64 |
Return record with ID C<$mfn> from database |
65 |
|
66 |
my $rec = $input->fetch_rec( $mfn, $filter_coderef ); |
67 |
|
68 |
=cut |
69 |
|
70 |
sub fetch_rec { |
71 |
my ( $self, $mfn, $filter_coderef ) = @_; |
72 |
|
73 |
return $self->{_rec}->[$mfn-1]; |
74 |
} |
75 |
|
76 |
|
77 |
=head2 size |
78 |
|
79 |
Return number of records in database |
80 |
|
81 |
my $size = $input->size; |
82 |
|
83 |
=cut |
84 |
|
85 |
sub size { |
86 |
my $self = shift; |
87 |
return $self->{size}; |
88 |
} |
89 |
|
90 |
=head1 AUTHOR |
91 |
|
92 |
Dobrica Pavlinusic, C<< <dpavlin@rot13.org> >> |
93 |
|
94 |
=head1 COPYRIGHT & LICENSE |
95 |
|
96 |
Copyright 2010 Dobrica Pavlinusic, All Rights Reserved. |
97 |
|
98 |
This program is free software; you can redistribute it and/or modify it |
99 |
under the same terms as Perl itself. |
100 |
|
101 |
=cut |
102 |
|
103 |
1; # End of WebPAC::Input::TSV |