blib/lib/Importer/Zim/Base.pm

filter: Statementstmt 95.8% 92 / 96 Branchbran 74.2% 52 / 70 Conditioncond 51.5% 17 / 33 MC/DCmcdc 11.1% 2 / 18 Subroutinesub 100% 13 / 13 Podpod n/a totaltotal 76.5% 176 / 230 SCAR 41.3
CC
47
Cov
81%
_prepare_args
18.8
_import_opts
12.8
_expand_symbol
12.3
CRAP 62.4
↑ Summary blib/lib/Importer/Zim/Utils.pm »
1  
2 package Importer::Zim::Base;
3 5 $Importer::Zim::Base::VERSION = '0.11.0';
4 # ABSTRACT: Base module for Importer::Zim backends
5  
6 5 ▶use 5.010001;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 5 ▶use Module::Runtime ();
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 5 ▶use Importer::Zim::Utils qw(DEBUG carp croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 sub _prepare_args {
13 16 ▶ my $class = shift;
Subroutine _prepare_args: called CC=18 SCAR=29.4
14 16 ▶ my $package = shift
Branch: 1 branch
BranchTrueFalseTotal
my $package = shift() or croak("Usage: use $class MODULE => [%OPTS =>] EXPORTS...\n") 0 16 16
15 or croak qq{Usage: use $class MODULE => [\%OPTS =>] EXPORTS...\n};
16  
17 16 ▶ my $opts = _module_opts( ref $_[0] eq 'HASH' ? shift : {} );
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] eq 'HASH' ? : 8 8 16
18 16 ▶ my @version = exists $opts->{-version} ? ( $opts->{-version} ) : ();
Branch: 1 branch
BranchTrueFalseTotal
exists $opts->{'-version'} ? : 0 16 16
19 16 &Module::Runtime::use_module( $package, @version );
20  
21 16 my $can_export = _can_export($package);
22  
23 16 my ( @exports, %seen );
24 16 ▶ @_ = @{"${package}::EXPORT"} unless @_ || !${"${package}::"}{'EXPORT'};
Branch: 1 branch
BranchTrueFalseTotal
unless @_ or not do { "${package}::" }->{"EXPORT"} 4 12 16
Condition66%2/3
@_ or not do { "${package}::" }->{"EXPORT"}
l!l&&r!l&&!r
1204
MC/DC50%1/2
@_ or not do { "${package}::" }->{"EXPORT"}
@_ not do { "${package}::" }->{"EXPORT"}
Truth table66%2/3
A or not(B)
A= @_B= not do { "${package}::" }->{"EXPORT"}
ABresult
1X1
011
000
25 16 ▶ while (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 166 16 182
26 166 my @symbols = _expand_symbol( $package, shift );
27 166 ▶ my $opts = _import_opts( ref $_[0] eq 'HASH' ? shift : {}, $opts );
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] eq 'HASH' ? : 7 159 166
28 exists $opts->{-filter}
29 166 ▶ and @symbols = grep &{ $opts->{-filter} }, @symbols;
Branch: 1 branch
BranchTrueFalseTotal
exists $opts->{"-filter"} and @symbols = grep(&{$opts->{"-filter"};}, @symbols) 144 22 166
30 166 for my $symbol (@symbols) {
31 croak qq{"$symbol" is not exported by "$package"}
32 89 ▶ if $opts->{-strict} && !$can_export->{$symbol};
Branch: 1 branch
BranchTrueFalseTotal
if $opts->{"-strict"} and not $can_export->{$symbol} 0 89 89
Condition66%2/3
$opts->{"-strict"} and not $can_export->{$symbol}
!ll&&!rl&&r
2870
MC/DC0%0/2
$opts->{"-strict"} and not $can_export->{$symbol}
$opts->{"-strict"} not $can_export->{$symbol}
Truth table66%2/3
A and not(B)
A= $opts->{"-strict"}B= not $can_export->{$symbol}
ABresult
0X0
100
111
33 89 ▶ croak qq{Can't handle "$symbol"}
Branch: 1 branch
BranchTrueFalseTotal
if $symbol =~ /^[\$\@\%\*]/ 0 89 89
34 if $symbol =~ /^[\$\@\%\*]/;
35 89 my $sub = *{"${package}::${symbol}"}{CODE};
36 89 my $export = do {
37 89 ▶ local $_ = $opts->{-as} // $symbol;
Condition66%2/3
$opts->{"-as"} // $symbol
l!l&&r!l&&!r
6830
MC/DC0%0/2
$opts->{"-as"} // $symbol
$opts->{"-as"} $symbol
Truth table66%2/3
A // B
A= $opts->{"-as"}B= $symbol
ABresult
1X1
011
000
38 89 ▶ exists $opts->{-map} ? $opts->{-map}->() : $_;
Branch: 1 branch
BranchTrueFalseTotal
exists $opts->{'-map'} ? : 8 81 89
39 };
40 89 ▶ croak qq{Can't find "$symbol" in "$package"}
Branch: 1 branch
BranchTrueFalseTotal
unless $sub 0 89 89
41 unless $sub;
42 89 my $seen = $seen{$export}{$sub}++;
43 croak qq{Can't import as "$export" twice}
44 89 ▶ if keys %{ $seen{$export} } > 1;
Branch: 1 branch
BranchTrueFalseTotal
if keys %{$seen{$export};} > 1 0 89 89
45 89 ▶ unless ($seen) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($seen) 87 2 89
46 87 warn(qq{ Importing "${package}::${symbol}" as "$export"\n})
47 if DEBUG;
48 87 push @exports, { export => $export, code => $sub };
49 }
50 }
51 }
52 16 return @exports;
53 }
54  
55 sub _module_opts {
56 state $IS_MODULE_OPTION
57 16 ▶ = { map { ; "-$_" => 1 } qw(how filter map prefix strict version) };
Subroutine _module_opts: called CC=7 SCAR=20.8
58  
59 16 my %opts = ( -strict => !!1 );
60 16 my $o = $_[0];
61 16 ▶ $opts{-strict} = !!$o->{-strict} if exists $o->{-strict};
Branch: 1 branch
BranchTrueFalseTotal
if exists $o->{"-strict"} 1 15 16
62 16 ▶ exists $o->{-filter} and $opts{-filter} = $o->{-filter};
Branch: 1 branch
BranchTrueFalseTotal
exists $o->{"-filter"} and $opts{"-filter"} = $o->{"-filter"} 4 12 16
63 exists $o->{-map} and $opts{-map} = $o->{-map}
64 16 ▶ or exists $o->{-prefix} and $opts{-map} = sub { $o->{-prefix} . $_ };
Subroutine __ANON__: called CC=1 SCAR=0.9
Branch: 1 branch
BranchTrueFalseTotal
exists $o->{"-map"} and $opts{"-map"} = $o->{"-map"} or exists $o->{"-prefix"} and $opts{"-map"} = sub { $o->{"-prefix"} . $_; } 16 0 16
Condition44%4/9
exists $o->{'-map'} && ($opts{'-map'} = $o->{'-map'}) || exists $o->{'-prefix'} && ($opts{'-map'} = sub { $o->{'-prefix'} . $_; } )
l!l&&r!l&&!r
0160
exists $o->{"-map"} and $opts{"-map"} = $o->{"-map"}
!ll&&!rl&&r
1600
exists $o->{"-prefix"} and $opts{"-map"} = sub { $o->{"-prefix"} . $_; }
!ll&&!rl&&r
1330
MC/DC0%0/4
exists $o->{'-map'} && ($opts{'-map'} = $o->{'-map'}) || exists $o->{'-prefix'} && ($opts{'-map'} = sub { $o->{'-prefix'} . $_; } )
exists $o->{"-map"} $opts{"-map"} = $o->{"-map"} exists $o->{"-prefix"} $opts{"-map"} = sub { $o->{"-prefix"} . $_; }
Truth table0%0/7
A and B || C and D
A= exists $o->{"-map"}B= $opts{"-map"} = $o->{"-map"}C= exists $o->{"-prefix"}D= $opts{"-map"} = sub { $o->{"-prefix"} . $_; }
ABCDresult
11XX1
0X111
10111
0X0X0
0X100
100X0
10100
65  
66 16 ▶ if ( my @bad = grep { !$IS_MODULE_OPTION->{$_} } keys %$o ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my(@bad) = grep({not $IS_MODULE_OPTION->{$_};} keys %$o)) 0 16 16
67 0 carp qq{Ignoring unknown module options (@bad)\n};
68 }
69 16 return \%opts;
70 }
71  
72 # $opts = _import_opts($opts1, $m_opts);
73 sub _import_opts {
74 state $IS_IMPORT_OPTION
75 166 ▶ = { map { ; "-$_" => 1 } qw(as filter map prefix strict) };
Subroutine _import_opts: called CC=11 SCAR=25.5
76  
77 166 my %opts = ( -strict => !!1 );
78 exists $_[1]{-filter}
79 166 ▶ and $opts{-filter} = _expand_filter( $_[1]{-filter} );
Branch: 1 branch
BranchTrueFalseTotal
exists $_[1]{"-filter"} and $opts{"-filter"} = _expand_filter($_[1]{"-filter"}) 144 22 166
80 166 ▶ exists $_[1]{-map} and $opts{-map} = $_[1]{-map};
Branch: 1 branch
BranchTrueFalseTotal
exists $_[1]{"-map"} and $opts{"-map"} = $_[1]{"-map"} 6 160 166
81 166 ▶ exists $_[1]{-strict} and $opts{-strict} = $_[1]{-strict};
Branch: 1 branch
BranchTrueFalseTotal
exists $_[1]{"-strict"} and $opts{"-strict"} = $_[1]{"-strict"} 166 0 166
82 166 my $o = $_[0];
83 166 ▶ $opts{-as} = $o->{-as} if exists $o->{-as};
Branch: 1 branch
BranchTrueFalseTotal
if exists $o->{"-as"} 6 160 166
84 166 ▶ exists $o->{-filter} and $opts{-filter} = _expand_filter( $o->{-filter} );
Branch: 1 branch
BranchTrueFalseTotal
exists $o->{"-filter"} and $opts{"-filter"} = _expand_filter($o->{"-filter"}) 0 166 166
85 exists $o->{-map} and $opts{-map} = $o->{-map}
86 166 ▶ or exists $o->{-prefix} and $opts{-map} = sub { $o->{-prefix} . $_ };
Subroutine __ANON__: called CC=1 SCAR=0.9
Branch: 1 branch
BranchTrueFalseTotal
exists $o->{"-map"} and $opts{"-map"} = $o->{"-map"} or exists $o->{"-prefix"} and $opts{"-map"} = sub { $o->{"-prefix"} . $_; } 166 0 166
Condition44%4/9
exists $o->{'-map'} && ($opts{'-map'} = $o->{'-map'}) || exists $o->{'-prefix'} && ($opts{'-map'} = sub { $o->{'-prefix'} . $_; } )
l!l&&r!l&&!r
01660
exists $o->{"-map"} and $opts{"-map"} = $o->{"-map"}
!ll&&!rl&&r
16600
exists $o->{"-prefix"} and $opts{"-map"} = sub { $o->{"-prefix"} . $_; }
!ll&&!rl&&r
16510
MC/DC0%0/4
exists $o->{'-map'} && ($opts{'-map'} = $o->{'-map'}) || exists $o->{'-prefix'} && ($opts{'-map'} = sub { $o->{'-prefix'} . $_; } )
exists $o->{"-map"} $opts{"-map"} = $o->{"-map"} exists $o->{"-prefix"} $opts{"-map"} = sub { $o->{"-prefix"} . $_; }
Truth table0%0/7
A and B || C and D
A= exists $o->{"-map"}B= $opts{"-map"} = $o->{"-map"}C= exists $o->{"-prefix"}D= $opts{"-map"} = sub { $o->{"-prefix"} . $_; }
ABCDresult
11XX1
0X111
10111
0X0X0
0X100
100X0
10100
87 166 ▶ $opts{-strict} = !!$o->{-strict} if exists $o->{-strict};
Branch: 1 branch
BranchTrueFalseTotal
if exists $o->{"-strict"} 1 165 166
88  
89 166 ▶ if ( my @bad = grep { !$IS_IMPORT_OPTION->{$_} } keys %$o ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my(@bad) = grep({not $IS_IMPORT_OPTION->{$_};} keys %$o)) 0 166 166
90 0 carp qq{Ignoring unknown symbol options (@bad)\n};
91 }
92 166 return \%opts;
93 }
94  
95 sub _expand_filter {
96 144 ▶ my $filter = shift;
Subroutine _expand_filter: called CC=2 SCAR=6.9
97 144 ▶ ref $filter eq 'Regexp' ? sub {/$filter/} : $filter;
Subroutine __ANON__: called CC=1 SCAR=0.0
Branch: 1 branch
BranchTrueFalseTotal
ref $filter eq 'Regexp' ? : 72 72 144
98 }
99  
100 sub _expand_symbol {
101 166 ▶ return $_[1] unless ref $_[1] || $_[1] =~ /^[:&]/;
Subroutine _expand_symbol: called CC=9 SCAR=25.1
Branch: 1 branch
BranchTrueFalseTotal
unless ref $_[1] or $_[1] =~ /^[:&]/ 161 5 166
Condition66%2/3
ref $_[1] or $_[1] =~ /^[:&]/
l!l&&r!l&&!r
05161
MC/DC50%1/2
ref $_[1] or $_[1] =~ /^[:&]/
ref $_[1] $_[1] =~ /^[:&]/
Truth table66%2/3
A or B
A= ref $_[1]B= $_[1] =~ /^[:&]/
ABresult
1X1
011
000
102  
103 5 ▶ return map { /^&/ ? substr( $_, 1 ) : $_ } @{ $_[1] } if ref $_[1];
Branch: 2 branches
BranchTrueFalseTotal
if ref $_[1] 0 5 5
/^&/ ? : 0 0 0
104  
105 5 ▶ return substr( $_[1], 1 ) if $_[1] =~ /^&/;
Branch: 1 branch
BranchTrueFalseTotal
if $_[1] =~ /^&/ 2 3 5
106  
107 3 my ( $package, $tag ) = ( $_[0], substr( $_[1], 1 ) );
108 my $symbols
109 3 ▶ = ${"${package}::"}{'EXPORT_TAGS'} && ${"${package}::EXPORT_TAGS"}{$tag}
Branch: 1 branch
BranchTrueFalseTotal
my $symbols = do { "${package}::" }->{"EXPORT_TAGS"} && do { "${package}::EXPORT_TAGS" }->{$tag} or return $_[1] 0 3 3
Condition33%1/3
do { "${package}::" }->{'EXPORT_TAGS'} && do { "${package}::EXPORT_TAGS" }->{$tag}
!ll&&!rl&&r
003
MC/DC0%0/2
do { "${package}::" }->{'EXPORT_TAGS'} && do { "${package}::EXPORT_TAGS" }->{$tag}
do { "${package}::" }->{'EXPORT_TAGS'} do { "${package}::EXPORT_TAGS" }->{$tag}
Truth table33%1/3
A && B
A= do { "${package}::" }->{'EXPORT_TAGS'}B= do { "${package}::EXPORT_TAGS" }->{$tag}
ABresult
0X0
100
111
110 or return $_[1];
111 3 ▶ return map { /^&/ ? substr( $_, 1 ) : $_ } @$symbols;
Branch: 1 branch
BranchTrueFalseTotal
/^&/ ? : 0 6 6
112 }
113  
114 sub _can_export {
115 16 ▶ my $package = shift;
Subroutine _can_export: called CC=5 SCAR=16.1
116 16 my %exports;
117 16 ▶ for (
Branch: 1 branch
BranchTrueFalseTotal
do { "${package}::" }->{'EXPORT'} ? : 4 12 16
118 16 ▶ ( ${"${package}::"}{'EXPORT'} ? @{"${package}::EXPORT"} : () ),
Branch: 1 branch
BranchTrueFalseTotal
do { "${package}::" }->{'EXPORT_OK'} ? : 16 0 16
119 16 ( ${"${package}::"}{'EXPORT_OK'} ? @{"${package}::EXPORT_OK"} : () )
120 )
121 {
122 374 ▶ my $x = /^&/ ? substr( $_, 1 ) : $_;
Branch: 1 branch
BranchTrueFalseTotal
/^&/ ? : 12 362 374
123 374 $exports{$x}++;
124 }
125 16 return \%exports;
126 }
127  
128 5 ▶no Importer::Zim::Utils qw(DEBUG carp croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
129  
130 5 1;
131  
132 #pod =encoding utf8
133 #pod
134 #pod =head1 DESCRIPTION
135 #pod
136 #pod "The Earth is safe once more, GIR! Now let's go destroy it!"
137 #pod – Zim
138 #pod
139 #pod No public interface.
140 #pod
141 #pod =head1 DEBUGGING
142 #pod
143 #pod You can set the C<IMPORTER_ZIM_DEBUG> environment variable
144 #pod for get some diagnostics information printed to C<STDERR>.
145 #pod
146 #pod IMPORTER_ZIM_DEBUG=1
147 #pod
148 #pod =head1 SEE ALSO
149 #pod
150 #pod L<Importer::Zim>
151 #pod
152 #pod =cut
153  
154 __END__
↑ Summary blib/lib/Importer/Zim/Utils.pm »