| 1 | package Object::Remote::ModuleLoader; | ||||||||||
| 2 | |||||||||||
| 3 | BEGIN { | ||||||||||
| 4 | package Object::Remote::ModuleLoader::Hook; | ||||||||||
| 5 | use Moo; | ||||||||||
| 6 | use Object::Remote::Logging qw( :log :dlog ); | ||||||||||
| 7 | has sender => (is => 'ro', required => 1); | ||||||||||
| 8 | |||||||||||
| 9 | # unqualified INC forced into package main | ||||||||||
| 10 | sub Object::Remote::ModuleLoader::Hook::INC { | ||||||||||
| 11 | 0 | ▶ | my ($self, $module) = @_; | ||||||||
|
Subroutine
INC: not called CC=7 SCAR=40.3
Pod
undocumented
| |||||||||||
| 12 | 0 | ▶ | log_debug { "Loading $module via " . ref($self) }; | ||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||
| 13 | 0 | my $ret = eval { | |||||||||
| 14 | 0 | ▶ | if (my $code = $self->sender->source_for($module)) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 15 | 0 | open my $fh, '<', \$code; | |||||||||
| 16 | 0 | ▶ | Dlog_trace { "Module sender successfully sent code for '$module': $code" } $code; | ||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||
| 17 | 0 | return $fh; | |||||||||
| 18 | } | ||||||||||
| 19 | 0 | ▶ | log_trace { "Module sender did not return code for '$module'" }; | ||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||
| 20 | 0 | return; | |||||||||
| 21 | }; | ||||||||||
| 22 | 0 | ▶ | if ($@) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 23 | 0 | ▶ | log_trace { "Module sender blew up - $@" }; | ||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||
| 24 | 0 | ▶ | if ($@ =~ /Can't locate/) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 25 | # Fudge the error messge to make it work with | ||||||||||
| 26 | # Module::Runtime use_package_optimistically | ||||||||||
| 27 | # Module::Runtime wants - /\ACan't locate \Q$fn\E .+ at \Q@{[__FILE__]}\E line/ | ||||||||||
| 28 | # We could probably measure and hard-code this but that could easily | ||||||||||
| 29 | # be a forwards compatibility disaster, so do a quick search of caller | ||||||||||
| 30 | # with a reasonable range; we're already into a woefully inefficient | ||||||||||
| 31 | # situation here so a little defensiveness won't make things much worse | ||||||||||
| 32 | 0 | foreach my $i (4..20) { | |||||||||
| 33 | 0 | my ($package, $file, $line) = caller($i); | |||||||||
| 34 | 0 | ▶ | last unless $package; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 35 | 0 | ▶ | if ($package eq 'Module::Runtime') { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 36 | # we want to fill in the error message with the | ||||||||||
| 37 | # module runtime module call info. | ||||||||||
| 38 | 0 | $@ =~ s/(in \@INC.)/$1 at $file line $line/; | |||||||||
| 39 | 0 | last; | |||||||||
| 40 | } | ||||||||||
| 41 | } | ||||||||||
| 42 | } | ||||||||||
| 43 | 0 | die $@; | |||||||||
| 44 | } | ||||||||||
| 45 | 0 | return $ret; | |||||||||
| 46 | } | ||||||||||
| 47 | } | ||||||||||
| 48 | |||||||||||
| 49 | use Moo; | ||||||||||
| 50 | |||||||||||
| 51 | use Object::Remote::Logging qw( :log ); | ||||||||||
| 52 | |||||||||||
| 53 | has module_sender => (is => 'ro', required => 1); | ||||||||||
| 54 | |||||||||||
| 55 | has inc_hook => (is => 'lazy'); | ||||||||||
| 56 | |||||||||||
| 57 | sub _build_inc_hook { | ||||||||||
| 58 | 0 | ▶ | my ($self) = @_; | ||||||||
|
Subroutine
_build_inc_hook: not called CC=1 SCAR=6.9
| |||||||||||
| 59 | 0 | ▶ | log_debug { "Constructing module builder hook" }; | ||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||
| 60 | 0 | my $hook = Object::Remote::ModuleLoader::Hook->new(sender => $self->module_sender); | |||||||||
| 61 | 0 | ▶ | log_trace { "Done constructing module builder hook" }; | ||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||
| 62 | 0 | return $hook; | |||||||||
| 63 | } | ||||||||||
| 64 | |||||||||||
| 65 | 0 | ▶ | sub BUILD { shift->enable } | ||||||||
|
Subroutine
BUILD: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||
| 66 | |||||||||||
| 67 | sub enable { | ||||||||||
| 68 | 0 | ▶ | log_debug { "enabling module loader hook" }; | ||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
Subroutine
enable: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||
| 69 | 0 | push @INC, shift->inc_hook; | |||||||||
| 70 | 0 | return; | |||||||||
| 71 | } | ||||||||||
| 72 | |||||||||||
| 73 | sub disable { | ||||||||||
| 74 | 0 | ▶ | my ($self) = @_; | ||||||||
|
Subroutine
disable: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||
| 75 | 0 | ▶ | log_debug { "disabling module loader hook" }; | ||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||
| 76 | 0 | my $hook = $self->inc_hook; | |||||||||
| 77 | 0 | @INC = grep $_ ne $hook, @INC; | |||||||||
| 78 | 0 | return; | |||||||||
| 79 | } | ||||||||||
| 80 | |||||||||||
| 81 | 0 | ▶ | sub DEMOLISH { $_[0]->disable unless $_[1] } | ||||||||
|
Subroutine
DEMOLISH: not called CC=2 SCAR=17.9
Pod
undocumented
Branch: 1 branch
| |||||||||||
| 82 | |||||||||||
| 83 | 1; | ||||||||||