blib/lib/autouse.pm

filter: Statementstmt 79.1% 38 / 48 Branchbran 45.8% 11 / 24 Conditioncond 50.0% 5 / 10 MC/DCmcdc 20.0% 1 / 5 Subroutinesub 83.3% 5 / 6 Podpod 0.0% 0 / 2 totaltotal 63.1% 60 / 95 CC 17 SCAR 24.6
CC
17
Cov
66%
import
18.7
__ANON__
7.4
vet_import
5.3
CRAP 11.7
↑ Summary
1 package autouse;
2  
3 #use strict; # debugging only
4 1 ▶use 5.006; # use warnings
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 1 $autouse::VERSION = '1.11';
7  
8 1 ▶$autouse::DEBUG ||= 0;
Condition50%1/2
$DEBUG ||= 0
l!l
01
MC/DC0%0/1
$DEBUG ||= 0
$DEBUG
Truth table50%1/2
A
A= $DEBUG
Aresult
11
00
9  
10 sub vet_import ($);
11  
12 1 ▶sub croak {
Subroutine croak: called CC=1 SCAR=0.0
Pod undocumented
13 1 require Carp;
14 1 Carp::croak(@_);
15 }
16  
17 8 ▶sub import {
Subroutine import: called CC=11 SCAR=29.3
18 8 ▶ my $class = @_ ? shift : 'autouse';
Branch: 1 branch
BranchTrueFalseTotal
@_ ? : 8 0 8
19 8 ▶ croak "usage: use $class MODULE [,SUBS...]" unless @_;
Branch: 1 branch
BranchTrueFalseTotal
unless @_ 0 8 8
20 8 my $module = shift;
21  
22 8 (my $pm = $module) =~ s{::}{/}g;
23 8 $pm .= '.pm';
24 8 ▶ if (exists $INC{$pm}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $INC{$pm}) 5 3 8
25 5 vet_import $module;
26 4 local $Exporter::ExportLevel = $Exporter::ExportLevel + 1;
27 # $Exporter::Verbose = 1;
28 4 return $module->import(map { (my $f = $_) =~ s/\(.*?\)$//; $f } @_);
29 }
30  
31 # It is not loaded: need to do real work.
32 3 my $callpkg = caller(0);
33 3 ▶ print "autouse called from $callpkg\n" if $autouse::DEBUG;
Branch: 1 branch
BranchTrueFalseTotal
if $DEBUG 0 3 3
34  
35 3 my $index;
36 3 for my $f (@_) {
37 3 my $proto;
38 3 ▶ $proto = $1 if (my $func = $f) =~ s/\((.*)\)$//;
Branch: 1 branch
BranchTrueFalseTotal
if (my $func = $f) =~ s/\((.*)\)$// 0 3 3
39  
40 3 my $closure_import_func = $func; # Full name
41 3 my $closure_func = $func; # Name inside package
42 3 my $index = rindex($func, '::');
43 3 ▶ if ($index == -1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($index == -1) { } 3 0 3
44 3 $closure_import_func = "${callpkg}::$func";
45 } else {
46 0 $closure_func = substr $func, $index + 2;
47 0 ▶ croak "autouse into different package attempted"
Branch: 1 branch
BranchTrueFalseTotal
unless substr($func, 0, $index) eq $module 0 0 0
48 unless substr($func, 0, $index) eq $module;
49 }
50  
51 0 ▶ my $load_sub = sub {
Subroutine __ANON__: not called CC=3 SCAR=20.0
52 0 ▶ unless ($INC{$pm}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($INC{$pm}) 0 0 0
53 0 require $pm;
54 0 vet_import $module;
55 }
56 1 ▶ no warnings qw(redefine prototype);
Subroutine BEGIN: called CC=1 SCAR=0.0
57 0 *$closure_import_func = \&{"${module}::$closure_func"};
58 0 ▶ print "autousing $module; "
Branch: 1 branch
BranchTrueFalseTotal
if $DEBUG 0 0 0
59 ."imported $closure_func as $closure_import_func\n"
60 if $autouse::DEBUG;
61 0 goto &$closure_import_func;
62 3 };
63  
64 3 ▶ if (defined $proto) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $proto) { } 0 3 3
65 0 ▶ *$closure_import_func = eval "sub ($proto) { goto &\$load_sub }"
Condition0%0/2
eval "sub ($proto) { goto &\$load_sub }" || die()
l!l
00
MC/DC0%0/1
eval "sub ($proto) { goto &\$load_sub }" || die()
eval "sub ($proto) { goto &\$load_sub }"
Truth table0%0/2
A
A= eval "sub ($proto) { goto &\$load_sub }"
Aresult
11
00
66 || die;
67 } else {
68 3 *$closure_import_func = $load_sub;
69 }
70 }
71 }
72  
73 5 ▶sub vet_import ($) {
Subroutine vet_import: called CC=5 SCAR=16.7
Pod undocumented
74 5 my $module = shift;
75 5 ▶ if (my $import = $module->can('import')) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $import = $module->can("import")) 5 0 5
76 5 ▶ croak "autoused module $module has unique import() method"
Branch: 1 branch
BranchTrueFalseTotal
unless defined &Exporter::import and $import == \&Exporter::import || $import == \&UNIVERSAL::import 1 4 5
Condition66%4/6
defined &Exporter::import and $import == \&Exporter::import || $import == \&UNIVERSAL::import
!ll&&!rl&&r
014
$import == \&Exporter::import or $import == \&UNIVERSAL::import
l!l&&r!l&&!r
401
MC/DC33%1/3
defined &Exporter::import and $import == \&Exporter::import || $import == \&UNIVERSAL::import
defined &Exporter::import $import == \&Exporter::import $import == \&UNIVERSAL::import
Truth table50%2/4
A and B or C
A= defined &Exporter::importB= $import == \&Exporter::importC= $import == \&UNIVERSAL::import
ABCresult
0XX0
1000
11X1
1011
77 unless defined(&Exporter::import)
78 && ($import == \&Exporter::import ||
79 $import == \&UNIVERSAL::import)
80 }
81 }
82  
83 1 1;
84  
85 __END__
↑ Summary