blib/lib/Export/Lexical.pm

filter: Statementstmt 68.0% 17 / 25 Branchbran 0.0% 0 / 4 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 85.7% 6 / 7 Podpod n/a totaltotal 63.8% 23 / 36 SCAR 12.9
CC
3
Cov
59%
MODIFY_CODE_ATTRIBUTES
12.0
BEGIN
1.0
BEGIN
1.0
CRAP 3.6
« Build ↑ Summary
1 package Export::Lexical;
2  
3 1 ▶use 5.010;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use version; our $VERSION = qv('0.0.4');
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 1 ▶use B;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 my %Exports_for = ();
11 my %Modifier_for = (); # e.g., $Modifier_for{$pkg} = 'silent'
12  
13 sub MODIFY_CODE_ATTRIBUTES {
14 0 ▶ my ( $package, $coderef, @attrs ) = @_;
Subroutine MODIFY_CODE_ATTRIBUTES: not called CC=3 SCAR=24.8
15  
16 0 my @unused_attrs = ();
17  
18 0 ▶ while ( my $attr = shift @attrs ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $attr = shift @attrs) 0 0 0
19 0 ▶ if ( $attr ~~ /^Export_?Lexical$/i ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($attr ~~ qr/^Export_?Lexical$/i) { } 0 0 0
20 0 push @{ $Exports_for{$package} }, $coderef;
21 }
22 else {
23 0 push @unused_attrs, $attr;
24 }
25 }
26  
27 0 return @unused_attrs;
28 }
29  
30 sub import {
31 my ($class) = @_;
32  
33 my $caller = caller;
34 my $key = _get_key($caller);
35 my @params = ();
36  
37 {
38 # Export our subroutines, if necessary.
39 1 ▶ no strict 'refs'; ## no critic (ProhibitNoStrict)
Subroutine BEGIN: called CC=1 SCAR=0.0
40  
41 if ( !exists &{ $caller . '::MODIFY_CODE_ATTRIBUTES' } ) {
42 *{ $caller . '::MODIFY_CODE_ATTRIBUTES' } = \&MODIFY_CODE_ATTRIBUTES;
43 }
44  
45 if ( !exists &{ $caller . '::import' } ) {
46 *{ $caller . '::import' } = sub {
47 my ( $class, @args ) = @_;
48  
49 _export_all_to( $caller, scalar caller );
50  
51 $^H{$key} = @args ? ( join ',', @args ) : 1;
52 };
53 }
54  
55 if ( !exists &{ $caller . '::unimport' } ) {
56 *{ $caller . '::unimport' } = sub {
57 my ( $class, @args ) = @_;
58  
59 if ( @args ) {
60 # Leave the '1' on the front of the list from a previous 'use
61 # $module', as well as any subs previously imported.
62 $^H{$key} = join ',', $^H{$key}, map { "!$_" } @args;
63 }
64 else {
65 $^H{$key} = '';
66 }
67 };
68 }
69 }
70  
71 while ( my $_ = shift ) {
72 if ( /^:(silent|warn)$/ ) {
73 croak qq('$_' requested when '$Modifier_for{$caller}' already in use)
74 if $Modifier_for{$caller};
75  
76 $Modifier_for{$caller} = $_;
77 next;
78 }
79  
80 push @params, $_;
81 }
82 }
83  
84 sub _export_all_to {
85 my ( $from, $caller ) = @_;
86  
87 return if !exists $Exports_for{$from};
88  
89 for my $ref ( @{ $Exports_for{$from} } ) {
90 my $obj = B::svref_2object($ref);
91 my $pkg = $obj->GV->STASH->NAME;
92 my $sub = $obj->GV->NAME;
93 my $key = _get_key($pkg);
94  
95 no strict 'refs'; ## no critic (ProhibitNoStrict)
96 no warnings 'redefine';
97  
98 next if exists &{ $caller . '::' . $sub };
99  
100 *{ $caller . '::' . $sub } = sub {
101 my $hints = (caller(0))[10];
102  
103 given ( $hints->{$key} ) {
104 my $re = qr/\b!$sub\b/;
105  
106 when ( '' ) { return _fail( $pkg, $sub ); } # no $module
107 when ( /!$sub\b/ ) { return _fail( $pkg, $sub ); } # no $module '$sub'
108  
109 when ( /^1\b/ || /\b$sub\b/ ) { goto $ref; } # use $module
110 # use $module '$sub'
111 }
112 };
113 }
114 }
115  
116 sub _fail {
117 my ( $pkg, $sub ) = @_;
118  
119 given ( $Modifier_for{$pkg} ) {
120 when (':silent') { return }
121 when (':warn') { carp "$pkg\::$sub not allowed here" }
122  
123 croak "$pkg\::$sub not allowed here";
124 }
125 }
126  
127 sub _get_key {
128 my ($pkg) = @_;
129  
130 return __PACKAGE__ . '/' . $pkg;
131 }
132  
133 1;
134  
135 __END__
« Build ↑ Summary