Importer/Factpack.pm untested

filter: Statementstmt 0.0% 0 / 35 Branchbran 0.0% 0 / 10 Conditioncond 0.0% 0 / 5 MC/DCmcdc 0.0% 0 / 3 Subroutinesub 0.0% 0 / 8 Podpod 0.0% 0 / 6 totaltotal 0.0% 0 / 67 CC 6 SCAR 21.7
CC
6
Cov
0%
next
20.0
fetch
6.0
rows
6.0
CRAP 8.8
1 package Bot::Infobot::Importer::Factpack;
2 use strict;
3 use Fcntl qw(:seek);
4  
5 sub handle {
6 return $_[0] =~ m!\.fact$!i;
7 }
8  
9 sub new {
10 my $class = shift;
11 my $file = shift;
12 my $fh;
13 open($fh, $file) || die "Couldn't open $file for reading : $!\n";
14 my $rows = 0;
15 $rows++ while <$fh>;
16 seek $fh, 0, SEEK_SET;
17 return bless { _fh => $fh, _rows => $rows }, $class;
18 }
19  
20 sub fetch {
21 my ($self, $table, $key) = @_;
22 $self->{_table} = $table;
23 $self->{_key} = (defined $key)? $key : undef;
24 }
25  
26 sub rows {
27 my $self = shift;
28 return 0 if $self->{_table} ne 'is';
29 return $self->{_rows};
30 }
31  
32 sub next {
33 my $self = shift;
34 return unless $self->{_table} eq 'is';
35 my $fh = $self->{_fh};
36 while ( my $line = <$fh> ) {
37 chomp($line);
38 $line =~ s!\s*$!!; # trim
39 my ($left, $right) = split(/\s*=>\s*/, $line, 2);
40 next unless defined $left;
41 next if defined $self->{_key} && $left ne $self->{_key};
42 return { key => $left, value => $right };
43 }
44 return;
45 }
46  
47 sub finish {
48 my $self = shift;
49 close($self->{_fh});
50 }
51  
52  
53  
54 1;