blib/lib/No/Worries/Export.pm

filter: Statementstmt 86.1% 56 / 65 Branchbran 68.1% 15 / 22 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 10 / 10 Podpod 100% 1 / 1 totaltotal 83.6% 82 / 98 CC 12 SCAR 21.1
CC
12
Cov
82%
export_control
13.6
BEGIN
1.0
BEGIN
1.0
CRAP 8.2
« blib/lib/No/Worries/Stat.pm ↑ Summary blib/lib/No/Worries/Log.pm »
1 #+##############################################################################
2 # #
3 # File: No/Worries/Export.pm #
4 # #
5 # Description: symbol exporting without worries #
6 # #
7 #-##############################################################################
8  
9 #
10 # module definition
11 #
12  
13 package No::Worries::Export;
14 23 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 23 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 23 our $VERSION = "1.7";
17 23 our $REVISION = sprintf("%d.%02d", q$Revision: 1.7 $ =~ /(\d+)\.(\d+)/);
18  
19 #
20 # used modules
21 #
22  
23 23 ▶use Params::Validate qw(validate_with :types);
Subroutine BEGIN: called CC=1 SCAR=0.0
24  
25 #
26 # simple yet powerful export control
27 #
28  
29 385 ▶sub export_control ($$$@) {
Subroutine export_control: called CC=12 SCAR=26.1
Pod documented
30 385 my($callpkg, $pkg, $exported, @names) = @_;
31 385 my($name, $regexp, $ref);
32  
33 385 validate_with(
34 params => \@_,
35 spec => [ { type => SCALAR }, { type => SCALAR }, { type => HASHREF } ],
36 allow_extra => 1,
37 );
38 385 ▶ while (@names) {
Branch: 1 branch
BranchTrueFalseTotal
if (@names) 605 385 990
39 605 $name = shift(@names);
40 # special case for * and /.../ and x.y
41 605 ▶ if ($name eq "*") {
Branch: 3 branches
BranchTrueFalseTotal
if ($name eq '*') { } 19 586 605
elsif ($name =~ m[^/(.*)/$]) { } 0 586 586
elsif ($name =~ /^\d/) { } 0 586 586
42 19 unshift(@names, grep(!ref($exported->{$_}), keys(%{ $exported })));
43 19 next;
44 } elsif ($name =~ /^\/(.*)\/$/) {
45 0 $regexp = $1;
46 unshift(@names, grep(/$regexp/, grep(!ref($exported->{$_}),
47 0 keys(%{ $exported }))));
48 0 next;
49 } elsif ($name =~ /^\d/) {
50 # version checking via UNIVERSAL
51 0 $pkg->VERSION($name);
52 0 next;
53 }
54 die("\"$name\" is not exported by the $pkg module\n")
55 586 ▶ unless defined($exported->{$name});
Branch: 1 branch
BranchTrueFalseTotal
unless defined $exported->{$name} 0 586 586
56 586 $ref = ref($exported->{$name});
57 586 ▶ if ($ref eq "") {
Branch: 2 branches
BranchTrueFalseTotal
if ($ref eq '') { } 586 0 586
elsif ($ref eq 'CODE') { } 0 0 0
58 # normal symbol
59 586 ▶ if ($name =~ /^(\w+)$/) {
Branch: 4 branches
BranchTrueFalseTotal
if ($name =~ /^(\w+)$/) { } 486 100 586
elsif ($name =~ /^\$(\w+)$/) { } 98 2 100
elsif ($name =~ /^\@(\w+)$/) { } 1 1 2
elsif ($name =~ /^\%(\w+)$/) { } 1 0 1
60 # function
61 23 ▶ no strict qw(refs);
Subroutine BEGIN: called CC=1 SCAR=0.0
62 23 ▶ no warnings qw(once prototype);
Subroutine BEGIN: called CC=1 SCAR=0.0
63 486 *{"${callpkg}::${1}"} = \&{"${pkg}::${1}"};
64 } elsif ($name =~ /^\$(\w+)$/) {
65 # scalar
66 23 ▶ no strict qw(refs);
Subroutine BEGIN: called CC=1 SCAR=0.0
67 98 *{"${callpkg}::${1}"} = \${"${pkg}::${1}"};
68 } elsif ($name =~ /^\@(\w+)$/) {
69 # array
70 23 ▶ no strict qw(refs);
Subroutine BEGIN: called CC=1 SCAR=0.0
71 1 *{"${callpkg}::${1}"} = \@{"${pkg}::${1}"};
72 } elsif ($name =~ /^\%(\w+)$/) {
73 # hash
74 23 ▶ no strict qw(refs);
Subroutine BEGIN: called CC=1 SCAR=0.0
75 1 *{"${callpkg}::${1}"} = \%{"${pkg}::${1}"};
76 } else {
77 0 die("unsupported export by the $pkg module: $name\n");
78 }
79 } elsif ($ref eq "CODE") {
80 # special symbol
81 0 $exported->{$name}->($name);
82 } else {
83 0 die("unsupported export by the $pkg module: $name=$ref\n");
84 }
85 }
86 }
87  
88 #
89 # export control
90 #
91  
92 129 ▶sub import : method {
Subroutine import: called CC=1 SCAR=0.0
93 129 my($pkg, %exported);
94  
95 129 $pkg = shift(@_);
96 129 grep($exported{$_}++, qw(export_control));
97 129 export_control(scalar(caller()), $pkg, \%exported, @_);
98 }
99  
100 23 1;
101  
102 __DATA__
« blib/lib/No/Worries/Stat.pm ↑ Summary blib/lib/No/Worries/Log.pm »