| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||||||||||||||||
| 39 | 12302 | local $/ = $rs; | |||||||||||||||||||||||||||||||||||||||||
| 40 | |||||||||||||||||||||||||||||||||||||||||||
| 41 | 12302 | while (1) { | |||||||||||||||||||||||||||||||||||||||||
| 42 | 12302 | my $l = <$fh>; | |||||||||||||||||||||||||||||||||||||||||
| 43 | 12302 | ▶ | if ( defined($l) ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 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
Condition16%1/6
not $filter or $filter and &$filter($l)
$filter and &$filter($l)
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)
| |||||||||||||||||||||||||||||||||||||||||||
| 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
Condition33%1/3
@_ == 1 and not ref $_[0]
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]
| |||||||||||||||||||||||||||||||||||||||||||
| 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
Condition66%2/3
$self->fh and $args->{"file"}
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"}
| |||||||||||||||||||||||||||||||||||||||||||
| 74 | 14 | ▶ | $args->{file} //= $args->{file_or_fh} if($args->{file_or_fh}); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$args->{"file"} //= $args->{"file_or_fh"}
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"}
| |||||||||||||||||||||||||||||||||||||||||||
| 75 | # open file | ||||||||||||||||||||||||||||||||||||||||||
| 76 | 14 | ▶ | if ( $args->{file} ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 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 | ||||||||||||||||||||||||||||||||||||||||||