blib/lib/App/Zip/X.pm

filter: Statementstmt 75.8% 44 / 58 Branchbran 37.5% 9 / 24 Conditioncond 26.0% 6 / 23 MC/DCmcdc 0.0% 0 / 13 Subroutinesub 90.9% 10 / 11 Podpod 0.0% 0 / 3 totaltotal 52.2% 69 / 132 CC 21 SCAR 28.6
CC
21
Cov
56%
replace
30.0
run
27.3
extract
6.1
CRAP 17.5
↑ Summary
1 package App::Zip::X;
2 1 ▶use 5.10.0;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 1 ▶use Getopt::Long qw(GetOptionsFromArray);
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use Archive::Zip qw(AZ_OK);
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use XML::LibXML;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 1 our $VERSION = '1.01';
11  
12  
13 # constant integers to specify indentation modes -- see L<XML::LibXML>
14 1 ▶use constant XML_NO_INDENT => 0;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 1 ▶use constant XML_SIMPLE_INDENT => 1;
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17  
18 1 ▶sub run { # no new() method -- this method both creates the instance and executes the request
Subroutine run: called CC=13 SCAR=33.1
Pod undocumented
19 1 my ($class, @args) = @_;
20  
21 1 my $self = bless {}, $class;
22  
23 1 GetOptionsFromArray \@args, $self,
24 'unzip!', # unzip mode (default)
25 'zip!', # zip mode
26 'xml_indent!', # handle indentation of XML extracted files (default)
27  
28 'archive=s', # archive name (or first arg on command line)
29 'member=s', # member to extract (or second arg on command line)
30 ;
31  
32 # other syntax : archive name and member name from command line without options
33 1 ▶ $self->{archive} //= shift @args or die "unspecified ZIP archive";
Branch: 1 branch
BranchTrueFalseTotal
$self->{"archive"} //= shift @args or die "unspecified ZIP archive" 1 0 1
Condition33%1/3
$self->{"archive"} //= shift @args
l!l&&r!l&&!r
100
MC/DC0%0/2
$self->{"archive"} //= shift @args
$self->{"archive"} shift @args
Truth table33%1/3
A //= B
A= $self->{"archive"}B= shift @args
ABresult
1X1
011
000
34 1 ▶ $self->{member} //= shift @args or die "unspecified member to extract from $self->{zip}";
Branch: 1 branch
BranchTrueFalseTotal
$self->{"member"} //= shift @args or die qq[unspecified member to extract from $self->{"zip"}] 1 0 1
Condition33%1/3
$self->{"member"} //= shift @args
l!l&&r!l&&!r
100
MC/DC0%0/2
$self->{"member"} //= shift @args
$self->{"member"} shift @args
Truth table33%1/3
A //= B
A= $self->{"member"}B= shift @args
ABresult
1X1
011
000
35 1 ▶ !@args or die "don't undestand these args: ", join(", ", @args);
Branch: 1 branch
BranchTrueFalseTotal
@args and die "don't undestand these args: ", join(", ", @args) 0 1 1
36  
37 # default flags and consistency check
38 1 ▶ !($self->{zip} && $self->{unzip}) or die "options -zip and -unzip are mutually exclusive";
Branch: 1 branch
BranchTrueFalseTotal
($self->{"zip"} and $self->{"unzip"}) and die "options -zip and -unzip are mutually exclusive" 0 1 1
Condition33%1/3
$self->{"zip"} and $self->{"unzip"}
!ll&&!rl&&r
100
MC/DC0%0/2
$self->{"zip"} and $self->{"unzip"}
$self->{"zip"} $self->{"unzip"}
Truth table33%1/3
A and B
A= $self->{"zip"}B= $self->{"unzip"}
ABresult
0X0
100
111
39 1 ▶ $self->{unzip} //= 1 unless $self->{zip};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"zip"} 1 0 1
Condition50%1/2
$self->{"unzip"} //= 1
l!l
01
MC/DC0%0/1
$self->{"unzip"} //= 1
$self->{"unzip"}
Truth table50%1/2
A
A= $self->{"unzip"}
Aresult
11
00
40  
41 # open ZIP archive
42 1 $self->{zipper} = Archive::Zip->new;
43 1 ▶ $self->{zipper}->read($self->{archive}) == AZ_OK
Branch: 1 branch
BranchTrueFalseTotal
$self->{"zipper"}->read($self->{"archive"}) == 0 or die qq[cannot open ZIP archive $self->{"archive"}] 1 0 1
44 or die "cannot open ZIP archive $self->{archive}";
45  
46 # decide what to do
47 1 ▶ if ($self->{unzip}) { $self->extract() }
Branch: 2 branches
BranchTrueFalseTotal
if ($self->{'unzip'}) { } 1 0 1
elsif ($self->{'zip'}) { } 0 0 0
48 0 elsif ($self->{zip}) { $self->replace() }
49 0 else { die "neither -zip nor -unzip .. not clear what you want to do"}
50  
51 }
52  
53  
54  
55 1 ▶sub extract {
Subroutine extract: called CC=5 SCAR=18.1
Pod undocumented
56 1 my ($self) = @_;
57  
58 # get member contents
59 my $contents = $self->{zipper}->contents($self->{member})
60 1 ▶ or die "no member named '$self->{member}' in $self->{archive}";
Branch: 1 branch
BranchTrueFalseTotal
my $contents = $self->{"zipper"}->contents($self->{"member"}) or die qq[no member named '$self->{"member"}' in $self->{"archive"}] 1 0 1
61  
62 # add XML indentation if necessary
63 1 ▶ if ($self->{xml_indent} && ($self->{member} =~ /\.xml$/i || $contents =~ /^<\?xml/)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"xml_indent"} and $self->{"member"} =~ /\.xml$/i || $contents =~ /^<\?xml/) 1 0 1
Condition33%2/6
$self->{"xml_indent"} and $self->{"member"} =~ /\.xml$/i || $contents =~ /^<\?xml/
!ll&&!rl&&r
001
$self->{"member"} =~ /\.xml$/i or $contents =~ /^<\?xml/
l!l&&r!l&&!r
100
MC/DC0%0/3
$self->{"xml_indent"} and $self->{"member"} =~ /\.xml$/i || $contents =~ /^<\?xml/
$self->{"xml_indent"} $self->{"member"} =~ /\.xml$/i $contents =~ /^<\?xml/
Truth table25%1/4
A and B or C
A= $self->{"xml_indent"}B= $self->{"member"} =~ /\.xml$/iC= $contents =~ /^<\?xml/
ABCresult
0XX0
1000
11X1
1011
64 1 my $dom = XML::LibXML->load_xml(string => $contents, recover => 1);
65 1 $contents = $dom->toString(XML_SIMPLE_INDENT); # already utf8-encoded
66 }
67  
68 # write on STDOUT
69 1 binmode STDOUT, ':raw';
70 1 print $contents;
71 }
72  
73  
74 0 ▶sub replace {
Subroutine replace: not called CC=5 SCAR=34.0
Pod undocumented
75 0 my ($self) = @_;
76  
77 # slurp contents from file relative to current directory
78 0 local $/;
79 0 ▶ open my $fh, "<:raw", $self->{member} or die "open $self->{member}: $!";
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, "<:raw", $self->{"member"} or die qq[open $self->{"member"}: $!] 0 0 0
80 0 my $contents = <$fh>;
81 0 close $fh;
82  
83 # remove XML indentation if necessary
84 0 ▶ if ($self->{xml_indent} && ($self->{member} =~ /\.xml$/i || $contents =~ /^<\?xml/)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"xml_indent"} and $self->{"member"} =~ /\.xml$/i || $contents =~ /^<\?xml/) 0 0 0
Condition0%0/6
$self->{"xml_indent"} and $self->{"member"} =~ /\.xml$/i || $contents =~ /^<\?xml/
!ll&&!rl&&r
000
$self->{"member"} =~ /\.xml$/i or $contents =~ /^<\?xml/
l!l&&r!l&&!r
000
MC/DC0%0/3
$self->{"xml_indent"} and $self->{"member"} =~ /\.xml$/i || $contents =~ /^<\?xml/
$self->{"xml_indent"} $self->{"member"} =~ /\.xml$/i $contents =~ /^<\?xml/
Truth table0%0/4
A and B or C
A= $self->{"xml_indent"}B= $self->{"member"} =~ /\.xml$/iC= $contents =~ /^<\?xml/
ABCresult
0XX0
1000
11X1
1011
85 0 my $dom = XML::LibXML->load_xml(string => $contents);
86 0 $contents = $dom->toString(XML_NO_INDENT);
87 }
88  
89 # replace member in archive and save
90 0 my $zipper = $self->{zipper};
91 0 $zipper->removeMember($self->{member});
92 0 $zipper->addString($contents, $self->{member});
93 0 $zipper->overwrite;
94 }
95  
96  
97 1 1; # End of App::Zip::X
98  
99 __END__
↑ Summary