File Coverage

blib/lib/App/dategrep/Iterator/Uncompress.pm
Criterion Covered Total %
statement 26 27 96.3
branch 9 12 75.0
condition 4 6 66.6
subroutine 5 5 100.0
pod n/a
total 44 50 88.0


line stmt bran cond sub pod time code
1             package App::dategrep::Iterator::Uncompress;
2 9     9   29 use strict;
  9         9  
  9         211  
3 9     9   26 use warnings;
  9         9  
  9         153  
4 9     9   21 use Moo;
  9         9  
  9         33  
5 9     9   6807 use IPC::Cmd 'can_run';
  9         317436  
  9         2611  
6             extends 'App::dategrep::Iterator::Fh';
7              
8             has filename => ( is => 'ro', required => 1 );
9             has fh => ( is => 'lazy' );
10              
11             sub _build_fh {
12 4     4   344 my $self = shift;
13 4         6 my $fh;
14 4 100       40 if ( $self->filename =~ /\.(bz|bz2)$/ ) {
    50          
15 2 100 66     27 if ( $^O eq 'MSWin32' or !can_run('bzcat') ) {
16 1         690 require IO::Uncompress::Bunzip2;
17 1         3419 $fh = IO::Uncompress::Bunzip2->new( $self->filename )
18              
19             }
20             else {
21 1         283 my @uncompress = qw(bzcat);
22 1 50       2452 open( $fh, '-|', @uncompress, $self->filename )
23             or die "Can't open @uncompress: $!\n";
24             }
25             }
26             elsif ( $self->filename =~ /\.(gz|z)$/ ) {
27 2 100 66     20 if ( $^O eq 'MSWin32' or !can_run('gzip') ) {
28 1         882 require IO::Uncompress::Gunzip;
29 1         29169 $fh = IO::Uncompress::Gunzip->new( $self->filename );
30             }
31             else {
32 1         76378 my @uncompress = qw(gzip -c -d);
33 1 50       3274 open( $fh, '-|', @uncompress, $self->filename )
34             or die "Can't open @uncompress: $!\n";
35             }
36             }
37             else {
38 0         0 die "unknown ending for compressed file " . $self->filename . "\n";
39             }
40 4         2360 return $fh;
41             }
42              
43             1;