File Coverage

blib/lib/archlib.pm
Criterion Covered Total %
statement 37 58 63.7
branch 7 22 31.8
condition n/a
subroutine 10 11 90.9
pod 0 2 0.0
total 54 93 58.0


line stmt bran cond sub pod time code
1             package archlib;
2 2     2   1560 use strict;
  2         4  
  2         79  
3 2     2   12 use warnings;
  2         3  
  2         80  
4              
5             #use Archive::Tar;
6 2     2   11 use Carp qw/croak/;
  2         4  
  2         129  
7 2     2   11 use List::Util qw/first/;
  2         3  
  2         527  
8              
9             our $VERSION = '0.002';
10             our @ARCHINC;
11             our %CONF;
12             our $HAVE_ZIP = 0;
13              
14             BEGIN {
15 2 50   2   130 if( eval 'require Archive::Zip; 1' ) {
16 0         0 Archive::Zip->import(qw/:ERROR_CODES :CONSTANTS/);
17 0         0 $HAVE_ZIP = 1;
18             }
19             else {
20             # Fake the constants to satisfy the compiler...
21 2     0   400 *AZ_OK = sub { croak "Should not see" };
  0         0  
22             }
23             }
24              
25             # configure 'zip' handler
26             BEGIN {
27             $CONF{zip} = $HAVE_ZIP ? sub {
28 0         0 my ( $file ) = @_;
29 0         0 my $it = Archive::Zip->new;
30 0 0       0 croak "Could not open archive '$file'"
31             unless $it->read( $file ) == AZ_OK;
32              
33             return sub {
34 0         0 my ( $mod ) = @_;
35 0         0 my $member = $it->memberNamed($mod);
36 0 0       0 return unless $member;
37 0         0 return split /\n/, $it->contents($member);
38 0         0 };
39 0         0 } : sub { croak "Install Archive::Zip for zip support" },
40 2 50   2   774 }
41              
42             # Configure 'tar' handler.
43             BEGIN {
44 2 50   2   17 my @order = $ENV{ARCHLIB_TAR_ORDER}
45             ? split( /\s/, $ENV{ARCHLIB_TAR_ORDER} )
46             : qw(Archive::Peek::Libarchive Archive::Peek::External Archive::Tar);
47              
48 2         28 my $tarmod = first { eval "require $_; 1" } @order;
  2         129  
49              
50 2 50       13 if ( $tarmod eq 'Archive::Tar' ) {
51             $CONF{'tar'} = sub {
52 2         3 my ( $file ) = @_;
53 2         18 my $it = $tarmod->new;
54 2         41 $it->read( $file );
55              
56             return sub {
57 2         6 my ( $mod ) = @_;
58 2 50       14 return unless $it->contains_file($mod);
59 2         152 return split /\n/, $it->get_content($mod);
60 2         6314 };
61             }
62 2         757 }
63             else {
64             $CONF{'tar'} = sub {
65 0         0 my ( $file ) = @_;
66 0         0 my $it = $tarmod->new( filename => $file );
67              
68             return sub {
69 0         0 my ( $mod ) = @_;
70 0 0       0 return unless $it->file($mod);
71 0         0 return split /\n/, $it->file($mod);
72 0         0 };
73 0         0 };
74             }
75             }
76              
77             unshift @INC => sub {
78             my ( $sub, $mod ) = @_;
79              
80             for my $arch (@ARCHINC) {
81             my @data = $arch->( $mod );
82             next unless @data;
83              
84             return sub {
85             return 0 unless @data;
86             $_ = shift(@data);
87             return 1;
88             };
89             }
90             return;
91             };
92              
93             sub import {
94 2     2   17 my $class = shift;
95 2         10 $class->add_archive( $_ ) for @_;
96             }
97              
98             sub add_archive {
99 2     2 0 4 my $class = shift;
100 2         4 my ( $archive ) = @_;
101 2 50       43 croak "No such archive '$archive'"
102             unless -e $archive;
103              
104 2         8 my $type = $class->arch_type( $archive );
105              
106 2         7 push @ARCHINC => $CONF{$type}->( $archive );
107             }
108              
109             sub arch_type {
110 2     2 0 4 my $class = shift;
111 2         4 my ( $archive ) = @_;
112 2 50       21 return 'tar' if $archive =~ m/\.tar(\.bz2|\.gz)?$/i;
113 0 0         return 'zip' if $archive =~ m/\.zip$/;
114 0           croak "Unknown archive type: '$archive'";
115             }
116              
117              
118             1;
119              
120             __END__