| 1 | 7 | ▶ | use strict; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 2 | 7 | ▶ | use warnings FATAL => 'all'; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 3 | |||||||||||||
| 4 | package Exporter::Attributes; | ||||||||||||
| 5 | |||||||||||||
| 6 | # ABSTRACT: Export symbols by attributes | ||||||||||||
| 7 | |||||||||||||
| 8 | 7 | ▶ | use Exporter 5.72 (); | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 9 | 7 | ▶ | use Attribute::Universal 0.003; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 10 | 7 | ▶ | use Carp qw(croak); | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 11 | |||||||||||||
| 12 | 7 | our $VERSION = '0.002'; # VERSION | |||||||||||
| 13 | |||||||||||||
| 14 | 7 | our @EXPORT_OK = qw(import); | |||||||||||
| 15 | |||||||||||||
| 16 | 7 | my $symbols = {}; | |||||||||||
| 17 | |||||||||||||
| 18 | 7 | my %lists = ( | |||||||||||
| 19 | Exportable => 'export_ok', | ||||||||||||
| 20 | Exported => 'export', | ||||||||||||
| 21 | ); | ||||||||||||
| 22 | |||||||||||||
| 23 | 7 | my %sigil = ( | |||||||||||
| 24 | SCALAR => '$', | ||||||||||||
| 25 | ARRAY => '@', | ||||||||||||
| 26 | HASH => '%', | ||||||||||||
| 27 | CODE => '&', | ||||||||||||
| 28 | ); | ||||||||||||
| 29 | |||||||||||||
| 30 | 62 | ▶ | sub add { | ||||||||||
|
Subroutine
add: called CC=4 SCAR=13.9
| |||||||||||||
| 31 | 62 | my ( $package, $list, $name, @tags ) = @_; | |||||||||||
| 32 | 62 | ▶ | $symbols->{$package} //= { | ||||||||||
|
Condition100%2/2
$symbols->{$package} //= {"export", [], "export_ok", [], "export_tags", {}}
MC/DC100%1/1
$symbols->{$package} //= {"export", [], "export_ok", [], "export_tags", {}}
$symbols->{$package}
Truth table100%2/2
A
A= $symbols->{$package}
| |||||||||||||
| 33 | export => [], | ||||||||||||
| 34 | export_ok => [], | ||||||||||||
| 35 | export_tags => {}, | ||||||||||||
| 36 | }; | ||||||||||||
| 37 | 62 | push @{ $symbols->{$package}->{$list} } => $name; | |||||||||||
| 38 | 62 | ▶ | return unless @tags; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 39 | 48 | foreach my $tag (@tags) { | |||||||||||
| 40 | 60 | push @{ $symbols->{$package}->{export_tags}->{$tag} } => $name; | |||||||||||
| 41 | } | ||||||||||||
| 42 | } | ||||||||||||
| 43 | |||||||||||||
| 44 | 7 | ▶ | use namespace::clean; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 45 | |||||||||||||
| 46 | 62 | ▶ | sub ATTRIBUTE { | ||||||||||
|
Subroutine
ATTRIBUTE: called CC=2 SCAR=7.0
Pod
undocumented
| |||||||||||||
| 47 | 62 | my $attr = Attribute::Universal::to_hash(@_); | |||||||||||
| 48 | croak( | ||||||||||||
| 49 | "lexical symbols are not exportable, in $attr->{file} at line $attr->{line}" | ||||||||||||
| 50 | 62 | ▶ | ) unless ref $attr->{symbol}; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 51 | 62 | my $sigil = $sigil{ $attr->{type} }; | |||||||||||
| 52 | 62 | my $list = $lists{ $attr->{attribute} }; | |||||||||||
| 53 | 62 | my @tags = map { split /[\s,]+/ } grep defined, @{ $attr->{content} }; | |||||||||||
| 54 | 62 | add( $attr->{package}, $list, $sigil . $attr->{label}, @tags ); | |||||||||||
| 55 | } | ||||||||||||
| 56 | |||||||||||||
| 57 | 14 | ▶ | sub import { | ||||||||||
|
Subroutine
import: called CC=2 SCAR=6.9
| |||||||||||||
| 58 | 14 | my $class = $_[0]; | |||||||||||
| 59 | 14 | my $caller = scalar caller; | |||||||||||
| 60 | |||||||||||||
| 61 | 14 | ▶ | if ( $class eq __PACKAGE__ ) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 62 | 7 | goto &_my_import; | |||||||||||
| 63 | } | ||||||||||||
| 64 | else { | ||||||||||||
| 65 | 7 | goto &_your_import; | |||||||||||
| 66 | } | ||||||||||||
| 67 | } | ||||||||||||
| 68 | |||||||||||||
| 69 | 7 | ▶ | sub _my_import { | ||||||||||
|
Subroutine
_my_import: called CC=1 SCAR=0.0
| |||||||||||||
| 70 | 7 | my $class = $_[0]; | |||||||||||
| 71 | 7 | my $caller = scalar caller; | |||||||||||
| 72 | |||||||||||||
| 73 | 7 | Attribute::Universal->import_into( | |||||||||||
| 74 | $caller, | ||||||||||||
| 75 | Exportable => 'ANY,BEGIN', | ||||||||||||
| 76 | Exported => 'ANY,BEGIN', | ||||||||||||
| 77 | ); | ||||||||||||
| 78 | 7 | goto &Exporter::import; | |||||||||||
| 79 | } | ||||||||||||
| 80 | |||||||||||||
| 81 | 7 | ▶ | sub _your_import { | ||||||||||
|
Subroutine
_your_import: called CC=2 SCAR=6.9
| |||||||||||||
| 82 | 7 | my $class = $_[0]; | |||||||||||
| 83 | 7 | my $caller = scalar caller; | |||||||||||
| 84 | |||||||||||||
| 85 | # get export symbols or just return | ||||||||||||
| 86 | 7 | ▶ | my $_symbols = $symbols->{$class} // return; | ||||||||||
|
Condition50%1/2
$symbols->{$class} // (return)
MC/DC0%0/1
$symbols->{$class} // (return)
$symbols->{$class}
Truth table50%1/2
A
A= $symbols->{$class}
| |||||||||||||
| 87 | |||||||||||||
| 88 | # build :all export tag by concat @EXPORT and @EXPORT_OK | ||||||||||||
| 89 | $_symbols->{export_tags}->{all} = | ||||||||||||
| 90 | 7 | [ @{ $_symbols->{export} }, @{ $_symbols->{export_ok} }, ]; | |||||||||||
| 91 | |||||||||||||
| 92 | # this is a quite easy way to say "our @Class::EXPORT", which is normally not possible | ||||||||||||
| 93 | # we are rewriting the symbol table, dont let strict concern about it! | ||||||||||||
| 94 | 7 | ▶ | no strict 'refs'; ## no critic | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 95 | 7 | *{"${class}::EXPORT"} = $_symbols->{export}; | |||||||||||
| 96 | 7 | *{"${class}::EXPORT_OK"} = $_symbols->{export_ok}; | |||||||||||
| 97 | 7 | *{"${class}::EXPORT_TAGS"} = $_symbols->{export_tags}; | |||||||||||
| 98 | |||||||||||||
| 99 | # and finally let import the symbol into the caller namespace. | ||||||||||||
| 100 | 7 | goto &Exporter::import; | |||||||||||
| 101 | } | ||||||||||||
| 102 | |||||||||||||
| 103 | 7 | 1; | |||||||||||
| 104 | |||||||||||||
| 105 | __END__ | ||||||||||||