blib/lib/Bio/Gonzales/Util/Role/FileIO.pm

filter: Statementstmt 98.5% 69 / 70 Branchbran 77.7% 14 / 18 Conditioncond 33.3% 5 / 15 MC/DCmcdc 0.0% 0 / 9 Subroutinesub 100% 15 / 15 Podpod 50.0% 1 / 2 totaltotal 80.6% 104 / 129 CC 15 SCAR 14.1
CC
15
Cov
85%
__ANON__
7.6
__ANON__
6.7
__ANON__
3.6
CRAP 4.1
« Build ↑ Summary blib/lib/Bio/Gonzales/Range/Cluster.pm »
1 package Bio::Gonzales::Util::Role::FileIO;
2  
3 7 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 7 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 7 ▶use Mouse::Role;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 7 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 7 ▶use Bio::Gonzales::Util::File qw/open_on_demand/;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 7 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 7 ▶use IO::Handle;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 7 ▶use IO::Zlib;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 7 our $VERSION = '0.058'; # VERSION
14  
15 7 has fh => ( is => 'rw' );
16 7 has mode => ( is => 'rw', default => '<' );
17 7 has _fhi => ( is => 'rw', lazy_build => 1 );
18 14 ▶has _cached_records => ( is => 'rw', default => sub { [] } );
Subroutine __ANON__: called CC=1 SCAR=0.0
19 7 has record_separator => ( is => 'rw', default => $/ );
20 7 has record_filter => ( is => 'rw' );
21 7 has _fh_was_open => ( is => 'rw', default => 1 );
22  
23 7 requires 'BUILDARGS';
24  
25 # file handle iterator
26 sub _build__fhi {
27 12 ▶ my ($self) = @_;
Subroutine _build__fhi: called CC=1 SCAR=0.1
28  
29 12 my $fh = $self->fh;
30  
31 12 my $rs = $self->record_separator;
32 12 my $filter = $self->record_filter;
33  
34 return sub {
35 # make use of cached records if we have
36 58 return shift @{ $self->_cached_records }
37 12360 ▶ if ( @{ $self->_cached_records } > 0 );
Subroutine __ANON__: called CC=6 SCAR=19.1
Branch: 1 branch
BranchTrueFalseTotal
if @{$self->_cached_records;} > 0 58 12302 12360
38  
39 12302 local $/ = $rs;
40  
41 12302 while (1) {
42 12302 my $l = <$fh>;
43 12302 ▶ if ( defined($l) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $l) { } 12284 18 12302
44 12284 $l =~ s/\r\n/\n/;
45 12284 chomp $l;
46 } else {
47 18 return;
48 }
49  
50 12284 ▶ return $l
Branch: 1 branch
BranchTrueFalseTotal
if not $filter or $filter and &$filter($l) 12284 0 12284
Condition16%1/6
not $filter or $filter and &$filter($l)
l!l&&r!l&&!r
1228400
$filter and &$filter($l)
!ll&&!rl&&r
000
MC/DC0%0/3
not $filter or $filter and &$filter($l)
not $filter $filter &$filter($l)
Truth table25%1/4
not(A) or B and C
A= not $filterB= $filterC= &$filter($l)
ABCresult
1XX1
0111
00X0
0100
51 if ( !$filter || ( $filter && $filter->($l) ) );
52 }
53 12 };
54 }
55  
56 around BUILDARGS => sub {
57 14 ▶ my $orig = shift;
Subroutine __ANON__: called CC=3 SCAR=12.7
58 14 my $class = shift;
59  
60 14 ▶ if ( @_ == 1 && !ref $_[0] ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ == 1 and not ref $_[0]) { } 0 14 14
Condition33%1/3
@_ == 1 and not ref $_[0]
!ll&&!rl&&r
1400
MC/DC0%0/2
@_ == 1 and not ref $_[0]
@_ == 1 not ref $_[0]
Truth table33%1/3
A and not(B)
A= @_ == 1B= not ref $_[0]
ABresult
0X0
100
111
61 0 return $class->$orig( file => $_[0] );
62 } else {
63 14 return $class->$orig(@_);
64 }
65 7 };
66  
67 ▶sub BUILD { }
Subroutine BUILD: called CC=1 SCAR=0.0
Pod undocumented
68  
69 before BUILD => sub {
70 14 ▶ my ( $self, $args ) = @_;
Subroutine __ANON__: called CC=7 SCAR=20.2
71  
72 confess "use either file, fh or file_or_fh" . Dumper $args
73 14 ▶ if ( $self->fh && $args->{file} );
Branch: 1 branch
BranchTrueFalseTotal
if $self->fh and $args->{"file"} 0 14 14
Condition66%2/3
$self->fh and $args->{"file"}
!ll&&!rl&&r
950
MC/DC0%0/2
$self->fh and $args->{"file"}
$self->fh $args->{"file"}
Truth table66%2/3
A and B
A= $self->fhB= $args->{"file"}
ABresult
0X0
100
111
74 14 ▶ $args->{file} //= $args->{file_or_fh} if($args->{file_or_fh});
Branch: 1 branch
BranchTrueFalseTotal
if $args->{"file_or_fh"} 1 13 14
Condition33%1/3
$args->{"file"} //= $args->{"file_or_fh"}
l!l&&r!l&&!r
010
MC/DC0%0/2
$args->{"file"} //= $args->{"file_or_fh"}
$args->{"file"} $args->{"file_or_fh"}
Truth table33%1/3
A //= B
A= $args->{"file"}B= $args->{"file_or_fh"}
ABresult
1X1
011
000
75 # open file
76 14 ▶ if ( $args->{file} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($args->{'file'}) { } 9 5 14
77 9 my ( $fh, $was_open ) = open_on_demand( $args->{file}, $self->mode );
78 9 $self->fh($fh);
79 9 $self->_fh_was_open($was_open);
80 } else {
81 5 ▶ confess "You did not supply a file handle for fh: " . ref $self->fh
Branch: 1 branch
BranchTrueFalseTotal
unless Bio::Gonzales::Util::File::is_fh($self->fh) 0 5 5
82 unless ( Bio::Gonzales::Util::File::is_fh( $self->fh ) );
83 }
84 7 };
85  
86 sub close {
87 7 ▶ my ($self) = @_;
Subroutine close: called CC=2 SCAR=6.9
Pod documented
88  
89 7 my $fh = $self->fh;
90 7 ▶ $fh->close unless ( $self->_fh_was_open );
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_fh_was_open 2 5 7
91  
92 7 return;
93 }
94  
95 7 1;
96  
97 __END__
98  
99 =head1 NAME
100  
101 BaMo::Role::FileIO - File input & ouput interface for parser classes
102  
103 =head1 SYNOPSIS
104  
105 use Mouse;
106  
107 with 'BaMo::Role::FileIO';
108  
109 sub parse {
110 my ($self) = @_;
111 $fhi = $self->_fhi;
112  
113 while(my $line = $fhi->()) {
114 #parse a bit
115 if($line =~ /break/) {
116 # oh no, we parsed too much...
117 push @{$self->_cached_records}, $line;
118 #but we can reverse it
119 }
120 }
121 }
122  
123 =head1 DESCRIPTION
124  
125 Enhances the class that uses this role with a file handle iterator that is
126 capable of caching records (lines in most cases), in case you read too much.
127  
128 =head1 METHODS
129  
130 =over 4
131  
132 =item B<< $self->fh() >>
133  
134 Get or set the filehandle.
135  
136 =item B<< $self->_cached_records() >>
137  
138 You can push lines on @{$self->_cached_records} (they need to be chomped
139 already). The file handle iterator will use them first if you call it. The
140 file handle will not be touched until all cached lines are shifted.
141  
142 =item B<< $class->new(file => 'filename.xyz', mode => '<') >>
143  
144 Opens the file in the specified mode. Sets the C<fh> and C<_fhi> attribute (indirectly).
145  
146 =item B<< $self->_fhi() >>
147  
148 Get the file handle iterator.
149  
150 =item B<< $class->new(fh => $fh) >>
151  
152 =item B<< $self->close() >>
153  
154 Close the filehandle.
155  
156 =back
157  
158 =head1 SEE ALSO
159  
160 =head1 AUTHOR
161  
162 jw bargsten, C<< <joachim.bargsten at wur.nl> >>
163  
164 =cut
« Build ↑ Summary blib/lib/Bio/Gonzales/Range/Cluster.pm »