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