Commit | Line | Data |
542d5b5c |
1 | package Object::Remote::ModuleLoader; |
2 | |
3 | BEGIN { |
4 | package Object::Remote::ModuleLoader::Hook; |
5 | use Moo; |
624072a8 |
6 | use Object::Remote::Logging qw( :log :dlog ); |
daa3054c |
7 | use Try::Tiny; |
542d5b5c |
8 | has sender => (is => 'ro', required => 1); |
9 | |
10 | # unqualified INC forced into package main |
11 | sub Object::Remote::ModuleLoader::Hook::INC { |
12 | my ($self, $module) = @_; |
4a9fa1a5 |
13 | log_debug { "Loading $module via " . ref($self) }; |
daa3054c |
14 | try |
15 | { |
16 | if (my $code = $self->sender->source_for($module)) { |
17 | open my $fh, '<', \$code; |
18 | Dlog_trace { "Module sender successfully sent code for '$module': $code" } $code; |
19 | return $fh; |
20 | } |
21 | log_trace { "Module sender did not return code for '$module'" }; |
22 | return; |
23 | } |
24 | catch |
25 | { |
26 | log_trace { "Module sender blew up - $_" }; |
27 | if($_ =~ /Can't locate/) |
28 | { |
29 | # Fudge the error messge to make it work with |
30 | # Module::Runtime use_package_optimistically |
31 | # Module::Runtime wants - /\ACan't locate \Q$fn\E .+ at \Q@{[__FILE__]}\E line/ |
32 | my ($package, $file, $line) = caller(9); |
33 | s/(in \@INC.)/$1 at $file line $line/; |
34 | } |
35 | die $_; |
542d5b5c |
36 | } |
542d5b5c |
37 | } |
38 | } |
39 | |
40 | use Moo; |
41 | |
5e2b2229 |
42 | use Object::Remote::Logging qw( :log ); |
43 | |
542d5b5c |
44 | has module_sender => (is => 'ro', required => 1); |
45 | |
46 | has inc_hook => (is => 'lazy'); |
47 | |
48 | sub _build_inc_hook { |
49 | my ($self) = @_; |
5e2b2229 |
50 | log_debug { "Constructing module builder hook" }; |
37efeb68 |
51 | my $hook = Object::Remote::ModuleLoader::Hook->new(sender => $self->module_sender); |
52 | log_trace { "Done constructing module builder hook" }; |
55c0d020 |
53 | return $hook; |
542d5b5c |
54 | } |
55 | |
56 | sub BUILD { shift->enable } |
57 | |
58 | sub enable { |
4a9fa1a5 |
59 | log_debug { "enabling module loader hook" }; |
542d5b5c |
60 | push @INC, shift->inc_hook; |
61 | return; |
62 | } |
63 | |
64 | sub disable { |
65 | my ($self) = @_; |
4a9fa1a5 |
66 | log_debug { "disabling module loader hook" }; |
542d5b5c |
67 | my $hook = $self->inc_hook; |
68 | @INC = grep $_ ne $hook, @INC; |
69 | return; |
70 | } |
71 | |
72 | sub DEMOLISH { $_[0]->disable unless $_[1] } |
73 | |
74 | 1; |