/[webpac2]/trunk/lib/WebPAC/Input/TSV.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

Contents of /trunk/lib/WebPAC/Input/TSV.pm

Parent Directory Parent Directory | Revision Log Revision Log


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

  ViewVC Help
Powered by ViewVC 1.1.26