tab separated values input module
authorDobrica Pavlinusic <dpavlin@rot13.org>
Sat, 16 Oct 2010 17:38:47 +0000 (17:38 +0000)
committerDobrica Pavlinusic <dpavlin@rot13.org>
Sat, 16 Oct 2010 17:38:47 +0000 (17:38 +0000)
git-svn-id: svn+ssh://mjesec/home/dpavlin/svn/webpac2/trunk@1343 07558da8-63fa-0310-ba24-9fe276d99e06

lib/WebPAC/Input/TSV.pm [new file with mode: 0644]
t/2-input-tsv.t [new file with mode: 0755]

diff --git a/lib/WebPAC/Input/TSV.pm b/lib/WebPAC/Input/TSV.pm
new file mode 100644 (file)
index 0000000..ef6e375
--- /dev/null
@@ -0,0 +1,103 @@
+package WebPAC::Input::TSV;
+
+use warnings;
+use strict;
+
+use WebPAC::Input;
+use base qw/WebPAC::Common/;
+
+use Encode;
+use Data::Dump qw/dump/;
+
+=head1 NAME
+
+WebPAC::Input::TSV - tab separated values
+
+=cut
+
+=head1 FUNCTIONS
+
+=head2 new
+
+  my $input = new WebPAC::Input::TSV(
+       path => '/path/to/records.tsv',
+  );
+
+=back
+
+Default encoding of input file is C<utf-8>
+
+=cut
+
+sub new {
+       my $class = shift;
+       my $self = {@_};
+       bless($self, $class);
+
+       my $arg = {@_};
+
+       my $log = $self->_get_logger();
+
+       open( my $fh, '<:raw', $arg->{path} ) || $log->logconfess("can't open $arg->{path}: $!");
+
+       $self->{size} = 0;
+
+       while ( my $line = <$fh> ) {
+
+               my $rec;
+               $rec->{'000'} = [ ++$self->{size} ];
+
+               my $col = 'A';
+               $rec->{ $col++ } = Encode::decode_utf8( $_ ) foreach split(/\t/,$line);
+
+               push @{ $self->{_rec} }, $rec;
+
+       };
+
+       $log->debug("loaded ", $self->size, " records");
+
+       $self ? return $self : return undef;
+}
+
+=head2 fetch_rec
+
+Return record with ID C<$mfn> from database
+
+  my $rec = $input->fetch_rec( $mfn, $filter_coderef );
+
+=cut
+
+sub fetch_rec {
+       my ( $self, $mfn, $filter_coderef ) = @_;
+
+       return $self->{_rec}->[$mfn-1];
+}
+
+
+=head2 size
+
+Return number of records in database
+
+  my $size = $input->size;
+
+=cut
+
+sub size {
+       my $self = shift;
+       return $self->{size};
+}
+
+=head1 AUTHOR
+
+Dobrica Pavlinusic, C<< <dpavlin@rot13.org> >>
+
+=head1 COPYRIGHT & LICENSE
+
+Copyright 2010 Dobrica Pavlinusic, All Rights Reserved.
+
+This program is free software; you can redistribute it and/or modify it
+under the same terms as Perl itself.
+
+=cut
+
+1; # End of WebPAC::Input::TSV
diff --git a/t/2-input-tsv.t b/t/2-input-tsv.t
new file mode 100755 (executable)
index 0000000..71c684a
--- /dev/null
@@ -0,0 +1,41 @@
+#!/usr/bin/perl -w
+
+use strict;
+use lib 'lib';
+
+use Test::More tests => 63;
+
+BEGIN {
+use_ok( 'WebPAC::Test' );
+use_ok( 'WebPAC::Input' );
+use_ok( 'Encode' );
+use_ok( 'Devel::Peek' );
+}
+
+my $module = 'WebPAC::Input::TSV';
+diag "testing with $module";
+
+ok(my $input = new WebPAC::Input(
+       module => $module,
+       no_progress_bar => 1,
+       %LOG
+), "new");
+
+ok(my $db = $input->open(
+       path => "$abs_path/data/records-utf8.tsv"
+), "open");
+ok(my $size = $input->size, "size");
+cmp_ok( $size, '==', 11, 'size ok' );
+
+foreach my $mfn ( 1 ... $size ) {
+       my $rec = $input->fetch;
+       ok($rec, "fetch $mfn");
+       cmp_ok($rec->{'000'}->[0], '==', $mfn, 'has mfn');
+       cmp_ok($input->pos, '==', $mfn, "pos $mfn");
+
+       ok( my $txt = $rec->{'E'}, 'E' );
+       diag Dump( $txt ) if $debug;
+       ok( Encode::is_utf8( $txt, 1 ), 'utf8' );
+       diag "rec: ", dump($rec), "\n" if $debug;
+}
+