| 1 | 4 | ▶ | use strict; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 2 | 4 | ▶ | 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 | 4 | ▶ | use Exporter 5.72 (); | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 9 | 4 | use Attribute::Universal 0.002 Exportable => 'ANY,BEGIN', | |||||||||||
| 10 | 4 | ▶ | Exported => 'ANY,BEGIN'; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 11 | 4 | ▶ | use Carp qw(croak); | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 12 | |||||||||||||
| 13 | 4 | our $VERSION = '0.001'; # VERSION | |||||||||||
| 14 | |||||||||||||
| 15 | 4 | our @EXPORT_OK = qw(import); | |||||||||||
| 16 | |||||||||||||
| 17 | 4 | my $symbols = {}; | |||||||||||
| 18 | |||||||||||||
| 19 | 4 | my %lists = ( | |||||||||||
| 20 | Exportable => 'export_ok', | ||||||||||||
| 21 | Exported => 'export', | ||||||||||||
| 22 | ); | ||||||||||||
| 23 | |||||||||||||
| 24 | 4 | my %sigil = ( | |||||||||||
| 25 | SCALAR => '$', | ||||||||||||
| 26 | ARRAY => '@', | ||||||||||||
| 27 | HASH => '%', | ||||||||||||
| 28 | CODE => '&', | ||||||||||||
| 29 | ); | ||||||||||||
| 30 | |||||||||||||
| 31 | sub add { | ||||||||||||
| 32 | 32 | ▶ | my ( $package, $list, $name, @tags ) = @_; | ||||||||||
|
Subroutine
add: called CC=4 SCAR=13.9
| |||||||||||||
| 33 | 32 | ▶ | $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}
| |||||||||||||
| 34 | export => [], | ||||||||||||
| 35 | export_ok => [], | ||||||||||||
| 36 | export_tags => {}, | ||||||||||||
| 37 | }; | ||||||||||||
| 38 | 32 | push @{ $symbols->{$package}->{$list} } => $name; | |||||||||||
| 39 | 32 | ▶ | return unless @tags; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 40 | 24 | foreach my $tag (@tags) { | |||||||||||
| 41 | 32 | push @{ $symbols->{$package}->{export_tags}->{$tag} } => $name; | |||||||||||
| 42 | } | ||||||||||||
| 43 | } | ||||||||||||
| 44 | |||||||||||||
| 45 | 4 | ▶ | use namespace::clean; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 46 | |||||||||||||
| 47 | sub ATTRIBUTE { | ||||||||||||
| 48 | 32 | ▶ | my $attr = Attribute::Universal::to_hash(@_); | ||||||||||
|
Subroutine
ATTRIBUTE: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||||
| 49 | 32 | my ( $package, $symbol, $referent, $attribute, $payload, $phase, $file, | |||||||||||
| 50 | $line ) | ||||||||||||
| 51 | = @_; | ||||||||||||
| 52 | croak("lexical symbols are not exportable, in $file at line $line") | ||||||||||||
| 53 | 32 | ▶ | unless ref $attr->{symbol}; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 54 | 32 | my $sigil = $sigil{ $attr->{type} }; | |||||||||||
| 55 | 32 | my $list = $lists{ $attr->{attribute} }; | |||||||||||
| 56 | 32 | my @tags = map { split /[\s,]+/ } @{ $attr->{payload} }; | |||||||||||
| 57 | 32 | add( $attr->{package}, $list, $sigil . $attr->{label}, @tags ); | |||||||||||
| 58 | } | ||||||||||||
| 59 | |||||||||||||
| 60 | sub import { | ||||||||||||
| 61 | 8 | ▶ | my $class = $_[0]; | ||||||||||
|
Subroutine
import: called CC=3 SCAR=11.0
| |||||||||||||
| 62 | |||||||||||||
| 63 | # export our own "import" method into the caller class | ||||||||||||
| 64 | # so abort here if "import" is called by "use Exporter::Attributes" | ||||||||||||
| 65 | 8 | ▶ | goto &Exporter::import if $class eq __PACKAGE__; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 66 | |||||||||||||
| 67 | # get export symbols or just return | ||||||||||||
| 68 | 4 | ▶ | 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}
| |||||||||||||
| 69 | |||||||||||||
| 70 | # build :all export tag by concat @EXPORT and @EXPORT_OK | ||||||||||||
| 71 | $_symbols->{export_tags}->{all} = | ||||||||||||
| 72 | 4 | [ @{ $_symbols->{export} }, @{ $_symbols->{export_ok} }, ]; | |||||||||||
| 73 | |||||||||||||
| 74 | # this is a quite easy way to say "our @Class::EXPORT", which is normally not possible | ||||||||||||
| 75 | # we are rewriting the symbol table, dont let strict concern about it! | ||||||||||||
| 76 | 4 | ▶ | no strict 'refs'; ## no critic | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 77 | 4 | *{"${class}::EXPORT"} = $_symbols->{export}; | |||||||||||
| 78 | 4 | *{"${class}::EXPORT_OK"} = $_symbols->{export_ok}; | |||||||||||
| 79 | 4 | *{"${class}::EXPORT_TAGS"} = $_symbols->{export_tags}; | |||||||||||
| 80 | |||||||||||||
| 81 | # and finally let import the symbol into the caller namespace. | ||||||||||||
| 82 | 4 | goto &Exporter::import; | |||||||||||
| 83 | } | ||||||||||||
| 84 | |||||||||||||
| 85 | 4 | 1; | |||||||||||
| 86 | |||||||||||||
| 87 | __END__ | ||||||||||||