| 1 | package deferred; | ||||||||||||||||||||||||||||
| 2 | 5 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 3 | |||||||||||||||||||||||||||||
| 4 | 5 | our $VERSION = "0.01"; | |||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||
| 6 | # Modules the user has requested to defer | ||||||||||||||||||||||||||||
| 7 | 5 | my @enabled; | |||||||||||||||||||||||||||
| 8 | # Modules we've half loaded | ||||||||||||||||||||||||||||
| 9 | my %half_loaded; | ||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||
| 11 | 5 | ▶ | sub import { | ||||||||||||||||||||||||||
|
Subroutine
import: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||
| 12 | 5 | my $class = shift; | |||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||
| 14 | 5 | ▶ | push @enabled, map { ref $_ ? $_ : qr/^$_$/ } @_; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 15 | } | ||||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||
| 17 | 2 | ▶ | sub unimport { | ||||||||||||||||||||||||||
|
Subroutine
unimport: called CC=8 SCAR=24.3
| |||||||||||||||||||||||||||||
| 18 | 2 | my $class = shift; | |||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||
| 20 | 2 | ▶ | my $discard = @_ && $_[0] eq '-discard' ? pop : 0; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
@_ and $_[0] eq "-discard"
MC/DC50%1/2
@_ and $_[0] eq "-discard"
@_
$_[0] eq "-discard"
Truth table66%2/3
A and B
A= @_B= $_[0] eq "-discard"
| |||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||
| 22 | 2 | ▶ | if(@_) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 23 | 0 | ▶ | my @disable = map { ref $_ ? $_ : qr/^$_$/ } @_; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||
| 25 | 0 | for my $disable(@disable) { | |||||||||||||||||||||||||||
| 26 | 0 | @enabled = grep { $_ ne $disable } @enabled; | |||||||||||||||||||||||||||
| 27 | } | ||||||||||||||||||||||||||||
| 28 | } else { | ||||||||||||||||||||||||||||
| 29 | 2 | @enabled = (); | |||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||
| 31 | 2 | ▶ | if(!$discard) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 32 | 1 | for my $class(keys %half_loaded) { | |||||||||||||||||||||||||||
| 33 | 1 | _load($class); | |||||||||||||||||||||||||||
| 34 | } | ||||||||||||||||||||||||||||
| 35 | } | ||||||||||||||||||||||||||||
| 36 | |||||||||||||||||||||||||||||
| 37 | 2 | %half_loaded = (); | |||||||||||||||||||||||||||
| 38 | } | ||||||||||||||||||||||||||||
| 39 | } | ||||||||||||||||||||||||||||
| 40 | |||||||||||||||||||||||||||||
| 41 | 93 | ▶ | unshift @INC, my $inc_ref = sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=4 SCAR=13.9
| |||||||||||||||||||||||||||||
| 42 | 93 | my(undef, $file) = @_; | |||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||
| 44 | # We get a filename here, we let the user specify a module name, so convert | ||||||||||||||||||||||||||||
| 45 | # it back. | ||||||||||||||||||||||||||||
| 46 | 93 | (my $module = $file) =~ s{/}{::}g; | |||||||||||||||||||||||||||
| 47 | 93 | $module =~ s/\.pm$//; | |||||||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||||||
| 49 | 93 | ▶ | if(caller =~ /^(?:base|parent)/) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 50 | # When these modules load something they really do mean it | ||||||||||||||||||||||||||||
| 51 | 4 | return; | |||||||||||||||||||||||||||
| 52 | } | ||||||||||||||||||||||||||||
| 53 | |||||||||||||||||||||||||||||
| 54 | 89 | for my $enabled(@enabled) { | |||||||||||||||||||||||||||
| 55 | 47 | ▶ | if($module =~ $enabled) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 56 | 5 | $half_loaded{$module} = join ":", (caller)[1,2]; | |||||||||||||||||||||||||||
| 57 | |||||||||||||||||||||||||||||
| 58 | 5 | open my $fh, "<", \"1"; | |||||||||||||||||||||||||||
| 59 | 5 | return $fh; | |||||||||||||||||||||||||||
| 60 | } | ||||||||||||||||||||||||||||
| 61 | } | ||||||||||||||||||||||||||||
| 62 | |||||||||||||||||||||||||||||
| 63 | 84 | return; | |||||||||||||||||||||||||||
| 64 | 5 | }; | |||||||||||||||||||||||||||
| 65 | |||||||||||||||||||||||||||||
| 66 | 421991 | ▶ | sub UNIVERSAL::AUTOLOAD { | ||||||||||||||||||||||||||
|
Subroutine
AUTOLOAD: called CC=5 SCAR=16.1
| |||||||||||||||||||||||||||||
| 67 | 421991 | my $load = $UNIVERSAL::AUTOLOAD; | |||||||||||||||||||||||||||
| 68 | |||||||||||||||||||||||||||||
| 69 | 421991 | my($class, $method) = ($load =~ /^(.*)::(.*)$/); | |||||||||||||||||||||||||||
| 70 | 421991 | ▶ | return if $method eq 'DESTROY'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 71 | |||||||||||||||||||||||||||||
| 72 | 4 | ▶ | _load($class) if exists $half_loaded{$class}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||
| 74 | 5 | ▶ | no warnings 'once'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 75 | 5 | ▶ | no strict 'refs'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 76 | |||||||||||||||||||||||||||||
| 77 | 3 | ▶ | if(*{$load}{CODE}) { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 78 | 1 | goto &$load; | |||||||||||||||||||||||||||
| 79 | } elsif(my $can = $class->can($method)) { | ||||||||||||||||||||||||||||
| 80 | 1 | goto &$can; | |||||||||||||||||||||||||||
| 81 | } else { | ||||||||||||||||||||||||||||
| 82 | # Really doesn't exist | ||||||||||||||||||||||||||||
| 83 | 1 | require Carp; | |||||||||||||||||||||||||||
| 84 | 1 | Carp::croak("Undefined subroutine/method called ($load)"); | |||||||||||||||||||||||||||
| 85 | } | ||||||||||||||||||||||||||||
| 86 | } | ||||||||||||||||||||||||||||
| 87 | |||||||||||||||||||||||||||||
| 88 | 4 | ▶ | sub _load { | ||||||||||||||||||||||||||
|
Subroutine
_load: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||
| 89 | 4 | my $class = shift; | |||||||||||||||||||||||||||
| 90 | |||||||||||||||||||||||||||||
| 91 | # Avoid the need to reimplement @INC searching | ||||||||||||||||||||||||||||
| 92 | 4 | ▶ | local @INC = grep { !ref || $_ != $inc_ref } @INC; | ||||||||||||||||||||||||||
|
Condition66%2/3
!ref($_) || $_ != $inc_ref
MC/DC50%1/2
!ref($_) || $_ != $inc_ref
!ref($_)
$_ != $inc_ref
Truth table66%2/3
not(A) || B
A= !ref($_)B= $_ != $inc_ref
| |||||||||||||||||||||||||||||
| 93 | |||||||||||||||||||||||||||||
| 94 | 4 | (my $file = $class) =~ s{::}{/}g; | |||||||||||||||||||||||||||
| 95 | 4 | $file .= ".pm"; | |||||||||||||||||||||||||||
| 96 | 4 | local %INC = %INC; | |||||||||||||||||||||||||||
| 97 | 4 | delete $INC{$file}; | |||||||||||||||||||||||||||
| 98 | |||||||||||||||||||||||||||||
| 99 | 4 | my $orig = delete $half_loaded{$class}; | |||||||||||||||||||||||||||
| 100 | 4 | my $ok = eval { require $file }; | |||||||||||||||||||||||||||
| 101 | |||||||||||||||||||||||||||||
| 102 | 4 | ▶ | die "deferred load of $class failed (originally loaded at $orig):\n$@" | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 103 | if !$ok; | ||||||||||||||||||||||||||||
| 104 | 3 | $ok; | |||||||||||||||||||||||||||
| 105 | } | ||||||||||||||||||||||||||||
| 106 | |||||||||||||||||||||||||||||
| 107 | 5 | 1; | |||||||||||||||||||||||||||
| 108 | |||||||||||||||||||||||||||||
| 109 | __END__ | ||||||||||||||||||||||||||||