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 8     8   28 use strict;
  8         9  
  8         174  
3 8     8   26 use warnings;
  8         6  
  8         133  
4 8     8   21 use Moo;
  8         8  
  8         29  
5 8     8   5966 use IPC::Cmd 'can_run';
  8         274481  
  8         2709  
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   418 my $self = shift;
13 4         4 my $fh;
14 4 100       44 if ( $self->filename =~ /\.(bz|bz2)$/ ) {
    50          
15 2 100 66     27 if ( $^O eq 'MSWin32' or !can_run('bzcat') ) {
16 1         627 require IO::Uncompress::Bunzip2;
17 1         3422 $fh = IO::Uncompress::Bunzip2->new( $self->filename )
18              
19             }
20             else {
21 1         340 my @uncompress = qw(bzcat);
22 1 50       3402 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     17 if ( $^O eq 'MSWin32' or !can_run('gzip') ) {
28 1         744 require IO::Uncompress::Gunzip;
29 1         26464 $fh = IO::Uncompress::Gunzip->new( $self->filename );
30             }
31             else {
32 1         67297 my @uncompress = qw(gzip -c -d);
33 1 50       4463 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         2141 return $fh;
41             }
42              
43             1;