Commit | Line | Data |
542d5b5c |
1 | package Object::Remote::ModuleLoader; |
2 | |
3 | BEGIN { |
4 | package Object::Remote::ModuleLoader::Hook; |
5 | use Moo; |
6 | has sender => (is => 'ro', required => 1); |
7 | |
8 | # unqualified INC forced into package main |
9 | sub Object::Remote::ModuleLoader::Hook::INC { |
10 | my ($self, $module) = @_; |
11 | if (my $code = $self->sender->source_for($module)) { |
12 | open my $fh, '<', \$code; |
13 | return $fh; |
14 | } |
15 | return; |
16 | } |
17 | } |
18 | |
19 | use Moo; |
20 | |
5e2b2229 |
21 | use Object::Remote::Logging qw( :log ); |
22 | |
542d5b5c |
23 | has module_sender => (is => 'ro', required => 1); |
24 | |
25 | has inc_hook => (is => 'lazy'); |
26 | |
27 | sub _build_inc_hook { |
28 | my ($self) = @_; |
5e2b2229 |
29 | log_debug { "Constructing module builder hook" }; |
30 | #TODO why didn't log_trace return the argument? |
31 | logS_trace { "Done constructing module builder hook" } |
32 | Object::Remote::ModuleLoader::Hook->new(sender => $self->module_sender); |
542d5b5c |
33 | } |
34 | |
35 | sub BUILD { shift->enable } |
36 | |
37 | sub enable { |
38 | push @INC, shift->inc_hook; |
39 | return; |
40 | } |
41 | |
42 | sub disable { |
43 | my ($self) = @_; |
44 | my $hook = $self->inc_hook; |
45 | @INC = grep $_ ne $hook, @INC; |
46 | return; |
47 | } |
48 | |
49 | sub DEMOLISH { $_[0]->disable unless $_[1] } |
50 | |
51 | 1; |