| 1 | package Import::Base; | ||||||||||||||||||||||||||||
| 2 | # ABSTRACT: Import a set of modules into the calling module | ||||||||||||||||||||||||||||
| 3 | 9 | our $VERSION = '1.004'; | |||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||
| 5 | 9 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | 9 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | 9 | ▶ | use mro (); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | 9 | ▶ | use Import::Into; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | 9 | ▶ | use Module::Runtime qw( use_module ); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||
| 11 | 9 | require Exporter; | |||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||
| 13 | 9 | our @ISA = qw(Exporter); | |||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||
| 15 | 46 | ▶ | sub modules { | ||||||||||||||||||||||||||
|
Subroutine
modules: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||||||||
| 16 | 46 | my ( $class, $bundles, $args ) = @_; | |||||||||||||||||||||||||||
| 17 | 46 | my @modules = (); | |||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||
| 19 | # Find all the modules from the static API | ||||||||||||||||||||||||||||
| 20 | # Reverse the array to allow more-specific classes to override | ||||||||||||||||||||||||||||
| 21 | # less-specific ones | ||||||||||||||||||||||||||||
| 22 | 46 | for my $pkg ( reverse @{ mro::get_linear_isa( $class ) } ) { | |||||||||||||||||||||||||||
| 23 | 9 | ▶ | no strict 'refs'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 24 | 9 | ▶ | no warnings 'once'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||
| 26 | 151 | ▶ | if ( !$args->{bundles_only} ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 27 | 145 | push @modules, @{ $pkg . "::IMPORT_MODULES" }; | |||||||||||||||||||||||||||
| 28 | } | ||||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||
| 30 | 151 | my %bundles = %{ $pkg . "::IMPORT_BUNDLES" }; | |||||||||||||||||||||||||||
| 31 | 23 | push @modules, map { @{ $bundles{ $_ } } } | |||||||||||||||||||||||||||
| 32 | 151 | grep { exists $bundles{ $_ } } | |||||||||||||||||||||||||||
| 33 | @$bundles; | ||||||||||||||||||||||||||||
| 34 | } | ||||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||
| 36 | 46 | return @modules; | |||||||||||||||||||||||||||
| 37 | } | ||||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||
| 39 | 46 | ▶ | sub _parse_args { | ||||||||||||||||||||||||||
|
Subroutine
_parse_args: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||
| 40 | 46 | my ( $class, @args ) = @_; | |||||||||||||||||||||||||||
| 41 | 46 | my @bundles; | |||||||||||||||||||||||||||
| 42 | 46 | ▶ | while ( @args ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 43 | 34 | ▶ | last if $args[0] =~ /^-/; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 44 | 31 | push @bundles, shift @args; | |||||||||||||||||||||||||||
| 45 | } | ||||||||||||||||||||||||||||
| 46 | 46 | my %args = @args; | |||||||||||||||||||||||||||
| 47 | 46 | return ( \@bundles, \%args ); | |||||||||||||||||||||||||||
| 48 | } | ||||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||
| 50 | 2 | ▶ | sub import_bundle { | ||||||||||||||||||||||||||
|
Subroutine
import_bundle: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 51 | 2 | my ( $class, @args ) = @_; | |||||||||||||||||||||||||||
| 52 | 2 | my ( $bundles, $args ) = $class->_parse_args( @args ); | |||||||||||||||||||||||||||
| 53 | |||||||||||||||||||||||||||||
| 54 | # Add internal Import::Base args | ||||||||||||||||||||||||||||
| 55 | 2 | $args->{package} = scalar caller(0); | |||||||||||||||||||||||||||
| 56 | 2 | $args->{bundles_only} = 1; | |||||||||||||||||||||||||||
| 57 | |||||||||||||||||||||||||||||
| 58 | 2 | my @modules = $class->modules( $bundles, $args ); | |||||||||||||||||||||||||||
| 59 | 2 | $class->_import_modules( \@modules, $bundles, $args ); | |||||||||||||||||||||||||||
| 60 | } | ||||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||
| 62 | 44 | ▶ | sub import { | ||||||||||||||||||||||||||
|
Subroutine
import: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 63 | 44 | my ( $class, @args ) = @_; | |||||||||||||||||||||||||||
| 64 | 44 | my ( $bundles, $args ) = $class->_parse_args( @args ); | |||||||||||||||||||||||||||
| 65 | |||||||||||||||||||||||||||||
| 66 | # Add internal Import::Base args | ||||||||||||||||||||||||||||
| 67 | 44 | $args->{package} = scalar caller(0); | |||||||||||||||||||||||||||
| 68 | |||||||||||||||||||||||||||||
| 69 | 44 | my @modules = $class->modules( $bundles, $args ); | |||||||||||||||||||||||||||
| 70 | 44 | $class->_import_modules( \@modules, $bundles, $args ); | |||||||||||||||||||||||||||
| 71 | } | ||||||||||||||||||||||||||||
| 72 | |||||||||||||||||||||||||||||
| 73 | 46 | ▶ | sub _import_modules { | ||||||||||||||||||||||||||
|
Subroutine
_import_modules: called CC=26 SCAR=32.6
| |||||||||||||||||||||||||||||
| 74 | 46 | my ( $class, $modules, $bundles, $args ) = @_; | |||||||||||||||||||||||||||
| 75 | |||||||||||||||||||||||||||||
| 76 | die "Argument to -exclude must be arrayref" | ||||||||||||||||||||||||||||
| 77 | 46 | ▶ | if $args->{-exclude} && ref $args->{-exclude} ne 'ARRAY'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$args->{"-exclude"} and ref $args->{"-exclude"} ne "ARRAY"
MC/DC100%2/2
$args->{"-exclude"} and ref $args->{"-exclude"} ne "ARRAY"
$args->{"-exclude"}
ref $args->{"-exclude"} ne "ARRAY"
Truth table100%3/3
A and B
A= $args->{"-exclude"}B= ref $args->{"-exclude"} ne "ARRAY"
| |||||||||||||||||||||||||||||
| 78 | 45 | my $exclude = {}; | |||||||||||||||||||||||||||
| 79 | 45 | ▶ | if ( $args->{-exclude} ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 80 | 2 | ▶ | while ( @{ $args->{-exclude} } ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 81 | 2 | my $module = shift @{ $args->{-exclude} }; | |||||||||||||||||||||||||||
| 82 | 2 | ▶ | my $subs = ref $args->{-exclude}[0] eq 'ARRAY' ? shift @{ $args->{-exclude} } : undef; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 83 | 2 | $exclude->{ $module } = $subs; | |||||||||||||||||||||||||||
| 84 | } | ||||||||||||||||||||||||||||
| 85 | } | ||||||||||||||||||||||||||||
| 86 | |||||||||||||||||||||||||||||
| 87 | # Add internal Import::Base args | ||||||||||||||||||||||||||||
| 88 | 45 | ▶ | $args->{package} ||= scalar caller(0); | ||||||||||||||||||||||||||
|
Condition50%1/2
$args->{"package"} ||= scalar caller 0
MC/DC0%0/1
$args->{"package"} ||= scalar caller 0
$args->{"package"}
Truth table50%1/2
A
A= $args->{"package"}
| |||||||||||||||||||||||||||||
| 89 | |||||||||||||||||||||||||||||
| 90 | 45 | my @modules = @$modules; | |||||||||||||||||||||||||||
| 91 | 45 | my @cb_args = ( $bundles, $args ); | |||||||||||||||||||||||||||
| 92 | |||||||||||||||||||||||||||||
| 93 | # Prepare the modules to load | ||||||||||||||||||||||||||||
| 94 | # First pass determines the order to load | ||||||||||||||||||||||||||||
| 95 | 45 | my ( @first_load, @last_load ); | |||||||||||||||||||||||||||
| 96 | 45 | ▶ | while ( @modules ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 97 | 145 | ▶ | if ( ref $modules[0] eq 'CODE' ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 98 | 15 | push @first_load, shift @modules; | |||||||||||||||||||||||||||
| 99 | 15 | next; | |||||||||||||||||||||||||||
| 100 | } | ||||||||||||||||||||||||||||
| 101 | |||||||||||||||||||||||||||||
| 102 | 130 | my $thing = shift @modules; | |||||||||||||||||||||||||||
| 103 | 130 | my $module; | |||||||||||||||||||||||||||
| 104 | my $version; | ||||||||||||||||||||||||||||
| 105 | 130 | ▶ | if ( ref $thing eq 'HASH' ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 106 | 4 | ( $module, $version ) = %$thing; | |||||||||||||||||||||||||||
| 107 | } | ||||||||||||||||||||||||||||
| 108 | else { | ||||||||||||||||||||||||||||
| 109 | 126 | $module = $thing; | |||||||||||||||||||||||||||
| 110 | } | ||||||||||||||||||||||||||||
| 111 | 130 | ▶ | my $imports = ref $modules[0] eq 'ARRAY' ? shift @modules : []; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 112 | |||||||||||||||||||||||||||||
| 113 | # Determine the module order | ||||||||||||||||||||||||||||
| 114 | 130 | ▶ | if ( $module =~ /^</ ) { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 115 | 10 | $module =~ s/^<//; | |||||||||||||||||||||||||||
| 116 | 10 | ▶ | if ( defined $version ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 117 | 0 | unshift @first_load, { $module => $version } => $imports; | |||||||||||||||||||||||||||
| 118 | } | ||||||||||||||||||||||||||||
| 119 | else { | ||||||||||||||||||||||||||||
| 120 | 10 | unshift @first_load, $module => $imports; | |||||||||||||||||||||||||||
| 121 | } | ||||||||||||||||||||||||||||
| 122 | } | ||||||||||||||||||||||||||||
| 123 | elsif ( $module =~ /^>/ ) { | ||||||||||||||||||||||||||||
| 124 | 10 | $module =~ s/^>//; | |||||||||||||||||||||||||||
| 125 | 10 | ▶ | if ( defined $version ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 126 | 0 | push @last_load, { $module => $version } => $imports; | |||||||||||||||||||||||||||
| 127 | } | ||||||||||||||||||||||||||||
| 128 | else { | ||||||||||||||||||||||||||||
| 129 | 10 | push @last_load, $module => $imports; | |||||||||||||||||||||||||||
| 130 | } | ||||||||||||||||||||||||||||
| 131 | } | ||||||||||||||||||||||||||||
| 132 | else { | ||||||||||||||||||||||||||||
| 133 | 110 | push @first_load, $thing => $imports; | |||||||||||||||||||||||||||
| 134 | } | ||||||||||||||||||||||||||||
| 135 | } | ||||||||||||||||||||||||||||
| 136 | |||||||||||||||||||||||||||||
| 137 | # Second pass loads the modules | ||||||||||||||||||||||||||||
| 138 | 45 | my @loads = ( @first_load, @last_load ); | |||||||||||||||||||||||||||
| 139 | 45 | ▶ | while ( my $load = shift @loads ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 140 | 153 | my $module; | |||||||||||||||||||||||||||
| 141 | my $version; | ||||||||||||||||||||||||||||
| 142 | 153 | ▶ | if ( ref $load eq 'CODE' ) { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 143 | 15 | unshift @loads, $load->( @cb_args ); | |||||||||||||||||||||||||||
| 144 | 12 | next; | |||||||||||||||||||||||||||
| 145 | } | ||||||||||||||||||||||||||||
| 146 | elsif ( ref $load eq 'HASH' ) { | ||||||||||||||||||||||||||||
| 147 | 4 | ( $module, $version ) = %$load; | |||||||||||||||||||||||||||
| 148 | } | ||||||||||||||||||||||||||||
| 149 | else { | ||||||||||||||||||||||||||||
| 150 | 134 | $module = $load; | |||||||||||||||||||||||||||
| 151 | } | ||||||||||||||||||||||||||||
| 152 | |||||||||||||||||||||||||||||
| 153 | 138 | ▶ | my $imports = ref $loads[0] eq 'ARRAY' ? shift @loads : []; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 154 | |||||||||||||||||||||||||||||
| 155 | 138 | ▶ | if ( exists $exclude->{ $module } ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 156 | 2 | ▶ | if ( defined $exclude->{ $module } ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 157 | 1 | my @left; | |||||||||||||||||||||||||||
| 158 | 1 | for my $import ( @$imports ) { | |||||||||||||||||||||||||||
| 159 | push @left, $import | ||||||||||||||||||||||||||||
| 160 | 1 | ▶ | unless grep { $_ eq $import } @{ $exclude->{ $module } }; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 161 | } | ||||||||||||||||||||||||||||
| 162 | 1 | $imports = \@left; | |||||||||||||||||||||||||||
| 163 | } | ||||||||||||||||||||||||||||
| 164 | else { | ||||||||||||||||||||||||||||
| 165 | 1 | next; | |||||||||||||||||||||||||||
| 166 | } | ||||||||||||||||||||||||||||
| 167 | } | ||||||||||||||||||||||||||||
| 168 | |||||||||||||||||||||||||||||
| 169 | 137 | my $method = 'import::into'; | |||||||||||||||||||||||||||
| 170 | 137 | ▶ | if ( $module =~ /^-/ ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 171 | 34 | $method = 'unimport::out_of'; | |||||||||||||||||||||||||||
| 172 | 34 | $module =~ s/^-//; | |||||||||||||||||||||||||||
| 173 | } | ||||||||||||||||||||||||||||
| 174 | |||||||||||||||||||||||||||||
| 175 | 137 | ▶ | if ($module->isa("Import::Base")) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 176 | 2 | $module->export_to_level( 2, $module, @{ $imports } ); | |||||||||||||||||||||||||||
| 177 | |||||||||||||||||||||||||||||
| 178 | 2 | next; | |||||||||||||||||||||||||||
| 179 | } | ||||||||||||||||||||||||||||
| 180 | |||||||||||||||||||||||||||||
| 181 | 135 | use_module( $module ); | |||||||||||||||||||||||||||
| 182 | 135 | ▶ | if ( defined $version ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 183 | 4 | $module->$method( { level => 2, version => $version }, @{ $imports } ); | |||||||||||||||||||||||||||
| 184 | } | ||||||||||||||||||||||||||||
| 185 | else { | ||||||||||||||||||||||||||||
| 186 | 131 | $module->$method( 2, @{ $imports } ); | |||||||||||||||||||||||||||
| 187 | } | ||||||||||||||||||||||||||||
| 188 | } | ||||||||||||||||||||||||||||
| 189 | } | ||||||||||||||||||||||||||||
| 190 | |||||||||||||||||||||||||||||
| 191 | 9 | 1; | |||||||||||||||||||||||||||
| 192 | |||||||||||||||||||||||||||||
| 193 | __END__ | ||||||||||||||||||||||||||||