| 1 | 2 | ▶ | use strict; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 2 | 2 | ▶ | use warnings; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 3 | |||||||||||||||||||||
| 4 | package Sub::Import 1.002; | ||||||||||||||||||||
| 5 | # ABSTRACT: import routines from most anything using Sub::Exporter | ||||||||||||||||||||
| 6 | |||||||||||||||||||||
| 7 | 2 | ▶ | use B qw(svref_2object); | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 8 | 2 | ▶ | use Carp (); | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 9 | 2 | ▶ | use Exporter (); | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 10 | 2 | ▶ | use Params::Util qw(_CLASS _CLASSISA); | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 11 | 2 | ▶ | use Sub::Exporter (); | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 12 | |||||||||||||||||||||
| 13 | #pod =head1 SYNOPSIS | ||||||||||||||||||||
| 14 | #pod | ||||||||||||||||||||
| 15 | #pod use Sub::Import 'Some::Library' => ( | ||||||||||||||||||||
| 16 | #pod some_routine => { -as => 'some_other_name' }, | ||||||||||||||||||||
| 17 | #pod other_routine => undef, | ||||||||||||||||||||
| 18 | #pod ); | ||||||||||||||||||||
| 19 | #pod | ||||||||||||||||||||
| 20 | #pod Some more examples: | ||||||||||||||||||||
| 21 | #pod | ||||||||||||||||||||
| 22 | #pod # import a function with a custom name | ||||||||||||||||||||
| 23 | #pod use Sub::Import 'Digest::MD5', md5_hex => {-as => 'md5sum'}; | ||||||||||||||||||||
| 24 | #pod | ||||||||||||||||||||
| 25 | #pod # import multiple functions, each with its own name | ||||||||||||||||||||
| 26 | #pod use Sub::Import 'MIME::Base64', | ||||||||||||||||||||
| 27 | #pod encode_base64 => {-as => 'e64'}, | ||||||||||||||||||||
| 28 | #pod decode_base64 => {-as => 'd64'}; | ||||||||||||||||||||
| 29 | #pod | ||||||||||||||||||||
| 30 | #pod # Import most functions with the "trig_" prefix, e.g. "trig_log", | ||||||||||||||||||||
| 31 | #pod # "trig_sin", "trig_cos", etc. | ||||||||||||||||||||
| 32 | #pod use Sub::Import 'Math::Trig', -all => {-prefix => 'trig_'}; | ||||||||||||||||||||
| 33 | #pod | ||||||||||||||||||||
| 34 | #pod # Import PI-related functions with the "_the_great" suffix, e.g. | ||||||||||||||||||||
| 35 | #pod # "pi_the_great", "pi2_the_great", etc. | ||||||||||||||||||||
| 36 | #pod use Sub::Import 'Math::Trig', -pi => {-suffix => '_the_great'}; | ||||||||||||||||||||
| 37 | #pod | ||||||||||||||||||||
| 38 | #pod =head1 DESCRIPTION | ||||||||||||||||||||
| 39 | #pod | ||||||||||||||||||||
| 40 | #pod Sub::Import is the companion to Sub::Exporter. You can use Sub::Import to get | ||||||||||||||||||||
| 41 | #pod Sub::Exporter-like import semantics, even if the library you're importing from | ||||||||||||||||||||
| 42 | #pod used Exporter.pm. | ||||||||||||||||||||
| 43 | #pod | ||||||||||||||||||||
| 44 | #pod The synopsis above should say it all. Where you would usually say: | ||||||||||||||||||||
| 45 | #pod | ||||||||||||||||||||
| 46 | #pod use Some::Library qw(foo bar baz); | ||||||||||||||||||||
| 47 | #pod | ||||||||||||||||||||
| 48 | #pod ...to get Exporter.pm semantics, you can now get Sub::Exporter semantics with: | ||||||||||||||||||||
| 49 | #pod | ||||||||||||||||||||
| 50 | #pod use Sub::Import 'Some::Library' => qw(foo bar baz); | ||||||||||||||||||||
| 51 | #pod | ||||||||||||||||||||
| 52 | #pod =head1 WARNINGS AND LIMITATIONS | ||||||||||||||||||||
| 53 | #pod | ||||||||||||||||||||
| 54 | #pod While you can rename imports, there is no way to customize them, because they | ||||||||||||||||||||
| 55 | #pod are not being built by generators. At present, extra arguments for each import | ||||||||||||||||||||
| 56 | #pod will be thrown away. In the future, they may become a fatal error. | ||||||||||||||||||||
| 57 | #pod | ||||||||||||||||||||
| 58 | #pod Non-subroutine imports will not be importable via this mechanism. | ||||||||||||||||||||
| 59 | #pod | ||||||||||||||||||||
| 60 | #pod The regex-like import features of Exporter.pm will be unavailable. (Will | ||||||||||||||||||||
| 61 | #pod anyone miss them?) | ||||||||||||||||||||
| 62 | #pod | ||||||||||||||||||||
| 63 | #pod =cut | ||||||||||||||||||||
| 64 | |||||||||||||||||||||
| 65 | 2 | ▶ | sub import { | ||||||||||||||||||
|
Subroutine
import: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 66 | 2 | my ($self, $target, @args) = @_; | |||||||||||||||||||
| 67 | |||||||||||||||||||||
| 68 | 2 | my $import = $self->_get_import($target); | |||||||||||||||||||
| 69 | |||||||||||||||||||||
| 70 | 2 | @_ = ($target, @args); | |||||||||||||||||||
| 71 | 2 | goto &$import; | |||||||||||||||||||
| 72 | } | ||||||||||||||||||||
| 73 | |||||||||||||||||||||
| 74 | 0 | ▶ | sub unimport { | ||||||||||||||||||
|
Subroutine
unimport: not called CC=1 SCAR=6.9
| |||||||||||||||||||||
| 75 | 0 | my ($self, $target, @args) = @_; | |||||||||||||||||||
| 76 | |||||||||||||||||||||
| 77 | 0 | my $unimport = $self->_get_unimport($target); | |||||||||||||||||||
| 78 | |||||||||||||||||||||
| 79 | 0 | @_ = ($target, @args); | |||||||||||||||||||
| 80 | 0 | goto &$unimport; | |||||||||||||||||||
| 81 | } | ||||||||||||||||||||
| 82 | |||||||||||||||||||||
| 83 | 0 | ▶ | sub _get_unimport { | ||||||||||||||||||
|
Subroutine
_get_unimport: not called CC=1 SCAR=6.9
| |||||||||||||||||||||
| 84 | 0 | my ($self, $target) = @_; | |||||||||||||||||||
| 85 | |||||||||||||||||||||
| 86 | 0 | $self->_get_methods($target)->{unimport}; | |||||||||||||||||||
| 87 | } | ||||||||||||||||||||
| 88 | |||||||||||||||||||||
| 89 | 2 | ▶ | sub _get_import { | ||||||||||||||||||
|
Subroutine
_get_import: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 90 | 2 | my ($self, $target) = @_; | |||||||||||||||||||
| 91 | |||||||||||||||||||||
| 92 | 2 | $self->_get_methods($target)->{import}; | |||||||||||||||||||
| 93 | } | ||||||||||||||||||||
| 94 | |||||||||||||||||||||
| 95 | 2 | my %GENERATED_METHODS; | |||||||||||||||||||
| 96 | 2 | ▶ | sub _get_methods { | ||||||||||||||||||
|
Subroutine
_get_methods: called CC=2 SCAR=8.1
| |||||||||||||||||||||
| 97 | 2 | my ($self, $target) = @_; | |||||||||||||||||||
| 98 | |||||||||||||||||||||
| 99 | 2 | ▶ | $GENERATED_METHODS{$target} ||= $self->_create_methods($target); | ||||||||||||||||||
|
Condition33%1/3
$GENERATED_METHODS{$target} ||= $self->_create_methods($target)
MC/DC0%0/2
$GENERATED_METHODS{$target} ||= $self->_create_methods($target)
$GENERATED_METHODS{$target}
$self->_create_methods($target)
Truth table33%1/3
A ||= B
A= $GENERATED_METHODS{$target}B= $self->_create_methods($target)
| |||||||||||||||||||||
| 100 | } | ||||||||||||||||||||
| 101 | |||||||||||||||||||||
| 102 | 2 | ▶ | sub _require_class { | ||||||||||||||||||
|
Subroutine
_require_class: called CC=3 SCAR=11.3
| |||||||||||||||||||||
| 103 | 2 | my ($self, $class) = @_; | |||||||||||||||||||
| 104 | |||||||||||||||||||||
| 105 | 2 | ▶ | Carp::croak("invalid package name: $class") unless _CLASS($class); | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 106 | |||||||||||||||||||||
| 107 | 2 | local $@; | |||||||||||||||||||
| 108 | 2 | ▶ | eval "require $class; 1" or die; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 109 | |||||||||||||||||||||
| 110 | 2 | return; | |||||||||||||||||||
| 111 | } | ||||||||||||||||||||
| 112 | |||||||||||||||||||||
| 113 | 2 | ▶ | sub _is_sexy { | ||||||||||||||||||
|
Subroutine
_is_sexy: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 114 | 2 | my ($self, $class) = @_; | |||||||||||||||||||
| 115 | |||||||||||||||||||||
| 116 | 2 | local $@; | |||||||||||||||||||
| 117 | 2 | my $isa; | |||||||||||||||||||
| 118 | 2 | my $ok = eval { | |||||||||||||||||||
| 119 | 2 | my $obj = svref_2object( $class->can('import') ); | |||||||||||||||||||
| 120 | 2 | my $importer_pkg = $obj->START->stashpv; | |||||||||||||||||||
| 121 | 2 | $isa = _CLASSISA($importer_pkg, 'Sub::Exporter'); | |||||||||||||||||||
| 122 | 2 | 1; | |||||||||||||||||||
| 123 | }; | ||||||||||||||||||||
| 124 | |||||||||||||||||||||
| 125 | 2 | return $isa; | |||||||||||||||||||
| 126 | } | ||||||||||||||||||||
| 127 | |||||||||||||||||||||
| 128 | my $EXPORTER_IMPORT; | ||||||||||||||||||||
| 129 | 2 | ▶ | BEGIN { $EXPORTER_IMPORT = Exporter->can('import'); } | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 130 | 1 | ▶ | sub _is_exporterrific { | ||||||||||||||||||
|
Subroutine
_is_exporterrific: called CC=2 SCAR=7.0
| |||||||||||||||||||||
| 131 | 1 | my ($self, $class) = @_; | |||||||||||||||||||
| 132 | |||||||||||||||||||||
| 133 | 1 | my $class_import = do { | |||||||||||||||||||
| 134 | 1 | local $@; | |||||||||||||||||||
| 135 | 1 | eval { $class->can('import') }; | |||||||||||||||||||
| 136 | }; | ||||||||||||||||||||
| 137 | |||||||||||||||||||||
| 138 | 1 | ▶ | return unless $class_import; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 139 | 1 | return $class_import == $EXPORTER_IMPORT; | |||||||||||||||||||
| 140 | } | ||||||||||||||||||||
| 141 | |||||||||||||||||||||
| 142 | 2 | ▶ | sub _create_methods { | ||||||||||||||||||
|
Subroutine
_create_methods: called CC=3 SCAR=11.2
| |||||||||||||||||||||
| 143 | 2 | my ($self, $target) = @_; | |||||||||||||||||||
| 144 | |||||||||||||||||||||
| 145 | 2 | $self->_require_class($target); | |||||||||||||||||||
| 146 | |||||||||||||||||||||
| 147 | 2 | ▶ | if ($self->_is_sexy($target)) { | ||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||
| 148 | return { | ||||||||||||||||||||
| 149 | 1 | import => $target->can("import"), | |||||||||||||||||||
| 150 | unimport => $target->can("unimport"), | ||||||||||||||||||||
| 151 | }; | ||||||||||||||||||||
| 152 | } elsif ($self->_is_exporterrific($target)) { | ||||||||||||||||||||
| 153 | 1 | return $self->_create_methods_exporter($target); | |||||||||||||||||||
| 154 | } else { | ||||||||||||||||||||
| 155 | 0 | return $self->_create_methods_fallback($target); | |||||||||||||||||||
| 156 | } | ||||||||||||||||||||
| 157 | } | ||||||||||||||||||||
| 158 | |||||||||||||||||||||
| 159 | 2 | ▶ | sub __filter_subs { | ||||||||||||||||||
|
Subroutine
__filter_subs: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 160 | 2 | my ($self, $exports) = @_; | |||||||||||||||||||
| 161 | |||||||||||||||||||||
| 162 | 2 | @$exports = map { s/\A&//; $_ } grep { /\A[&_a-z]/ } @$exports; | |||||||||||||||||||
| 163 | } | ||||||||||||||||||||
| 164 | |||||||||||||||||||||
| 165 | 1 | ▶ | sub _create_methods_exporter { | ||||||||||||||||||
|
Subroutine
_create_methods_exporter: called CC=2 SCAR=6.9
| |||||||||||||||||||||
| 166 | 1 | my ($self, $target) = @_; | |||||||||||||||||||
| 167 | |||||||||||||||||||||
| 168 | 2 | ▶ | no strict 'refs'; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 169 | |||||||||||||||||||||
| 170 | 1 | my @ok = @{ $target . "::EXPORT_OK" }; | |||||||||||||||||||
| 171 | 1 | my @default = @{ $target . "::EXPORT" }; | |||||||||||||||||||
| 172 | 1 | my %groups = %{ $target . "::EXPORT_TAGS" }; | |||||||||||||||||||
| 173 | |||||||||||||||||||||
| 174 | 1 | $self->__filter_subs($_) for (\@ok, \@default, values %groups); | |||||||||||||||||||
| 175 | |||||||||||||||||||||
| 176 | 1 | my @all = do { | |||||||||||||||||||
| 177 | 1 | my %seen; | |||||||||||||||||||
| 178 | 1 | grep { ! $seen{$_}++ } @ok, @default; | |||||||||||||||||||
| 179 | }; | ||||||||||||||||||||
| 180 | |||||||||||||||||||||
| 181 | 1 | my $import = Sub::Exporter::build_exporter({ | |||||||||||||||||||
| 182 | exports => \@all, | ||||||||||||||||||||
| 183 | groups => { | ||||||||||||||||||||
| 184 | %groups, | ||||||||||||||||||||
| 185 | default => \@default, | ||||||||||||||||||||
| 186 | } | ||||||||||||||||||||
| 187 | }); | ||||||||||||||||||||
| 188 | |||||||||||||||||||||
| 189 | return { | ||||||||||||||||||||
| 190 | import => $import, | ||||||||||||||||||||
| 191 | 0 | ▶ | unimport => sub { die "unimport not handled for Exporter via Sub::Import" }, | ||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||
| 192 | 1 | }; | |||||||||||||||||||
| 193 | } | ||||||||||||||||||||
| 194 | |||||||||||||||||||||
| 195 | 0 | ▶ | sub _create_methods_fallback { | ||||||||||||||||||
|
Subroutine
_create_methods_fallback: not called CC=1 SCAR=6.9
| |||||||||||||||||||||
| 196 | 0 | my ($self, @target) = @_; | |||||||||||||||||||
| 197 | |||||||||||||||||||||
| 198 | 0 | Carp::confess( | |||||||||||||||||||
| 199 | "Sub::Import only handles Sub::Exporter and Exporter-based import methods" | ||||||||||||||||||||
| 200 | ); | ||||||||||||||||||||
| 201 | } | ||||||||||||||||||||
| 202 | |||||||||||||||||||||
| 203 | 2 | 1; | |||||||||||||||||||
| 204 | |||||||||||||||||||||
| 205 | __END__ | ||||||||||||||||||||