1 |
dpavlin |
1343 |
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 |
dpavlin |
1347 |
chomp $line; |
47 |
dpavlin |
1343 |
|
48 |
|
|
my $rec; |
49 |
|
|
$rec->{'000'} = [ ++$self->{size} ]; |
50 |
|
|
|
51 |
|
|
my $col = 'A'; |
52 |
dpavlin |
1346 |
foreach my $v ( split(/\t/,$line) ) { |
53 |
dpavlin |
1349 |
$rec->{ $col } = Encode::decode_utf8( $v ) if $v ne '\N'; |
54 |
|
|
$col++; |
55 |
dpavlin |
1346 |
} |
56 |
dpavlin |
1343 |
|
57 |
|
|
push @{ $self->{_rec} }, $rec; |
58 |
|
|
|
59 |
|
|
}; |
60 |
|
|
|
61 |
|
|
$log->debug("loaded ", $self->size, " records"); |
62 |
|
|
|
63 |
|
|
$self ? return $self : return undef; |
64 |
|
|
} |
65 |
|
|
|
66 |
|
|
=head2 fetch_rec |
67 |
|
|
|
68 |
|
|
Return record with ID C<$mfn> from database |
69 |
|
|
|
70 |
|
|
my $rec = $input->fetch_rec( $mfn, $filter_coderef ); |
71 |
|
|
|
72 |
|
|
=cut |
73 |
|
|
|
74 |
|
|
sub fetch_rec { |
75 |
|
|
my ( $self, $mfn, $filter_coderef ) = @_; |
76 |
|
|
|
77 |
|
|
return $self->{_rec}->[$mfn-1]; |
78 |
|
|
} |
79 |
|
|
|
80 |
|
|
|
81 |
|
|
=head2 size |
82 |
|
|
|
83 |
|
|
Return number of records in database |
84 |
|
|
|
85 |
|
|
my $size = $input->size; |
86 |
|
|
|
87 |
|
|
=cut |
88 |
|
|
|
89 |
|
|
sub size { |
90 |
|
|
my $self = shift; |
91 |
|
|
return $self->{size}; |
92 |
|
|
} |
93 |
|
|
|
94 |
|
|
=head1 AUTHOR |
95 |
|
|
|
96 |
|
|
Dobrica Pavlinusic, C<< <dpavlin@rot13.org> >> |
97 |
|
|
|
98 |
|
|
=head1 COPYRIGHT & LICENSE |
99 |
|
|
|
100 |
|
|
Copyright 2010 Dobrica Pavlinusic, All Rights Reserved. |
101 |
|
|
|
102 |
|
|
This program is free software; you can redistribute it and/or modify it |
103 |
|
|
under the same terms as Perl itself. |
104 |
|
|
|
105 |
|
|
=cut |
106 |
|
|
|
107 |
|
|
1; # End of WebPAC::Input::TSV |