my ($self, $args) = @_;
unless ($self->id) {
die "No id supplied and no class either" unless $args->{class};
+ ref($_) eq 'HASH' and $_ = [ %$_ ] for $args->{args};
$self->_set_id(
$self->_await(
$self->connection->send(
--- /dev/null
+package Object::Remote::ModuleLoader;
+
+BEGIN {
+ package Object::Remote::ModuleLoader::Hook;
+ use Moo;
+ has sender => (is => 'ro', required => 1);
+
+ # unqualified INC forced into package main
+ sub Object::Remote::ModuleLoader::Hook::INC {
+ my ($self, $module) = @_;
+ if (my $code = $self->sender->source_for($module)) {
+ open my $fh, '<', \$code;
+ return $fh;
+ }
+ return;
+ }
+}
+
+use Moo;
+
+has module_sender => (is => 'ro', required => 1);
+
+has inc_hook => (is => 'lazy');
+
+sub _build_inc_hook {
+ my ($self) = @_;
+ Object::Remote::ModuleLoader::Hook->new(sender => $self->module_sender);
+}
+
+sub BUILD { shift->enable }
+
+sub enable {
+ push @INC, shift->inc_hook;
+ return;
+}
+
+sub disable {
+ my ($self) = @_;
+ my $hook = $self->inc_hook;
+ @INC = grep $_ ne $hook, @INC;
+ return;
+}
+
+sub DEMOLISH { $_[0]->disable unless $_[1] }
+
+1;
--- /dev/null
+package Object::Remote::ModuleSender;
+
+use Config;
+use File::Spec;
+use List::Util qw(first);
+use Moo;
+
+has dir_list => (is => 'lazy');
+
+sub _build_dir_list {
+ my %core = map +($_ => 1), @Config{qw(privlibexp archlibexp)};
+ [ grep !/$Config{archname}$/, grep !$core{$_}, @INC ];
+}
+
+sub source_for {
+ my ($self, $module) = @_;
+ my ($found) = first { -f $_ }
+ map File::Spec->catfile($_, $module),
+ @{$self->dir_list};
+ die "Couldn't find ${module} in remote \@INC. dir_list contains:\n"
+ .join("\n", @{$self->dir_list})
+ unless $found;
+ open my $fh, '<', $found or die "Couldn't open ${found} for ${module}: $!";
+ return do { local $/; <$fh> };
+}
+
+1;
--- /dev/null
+use strictures 1;
+use Test::More;
+
+use Object::Remote::Connector::Local;
+use Object::Remote;
+use Object::Remote::ModuleSender;
+
+$ENV{PERL5LIB} = join(
+ ':', ($ENV{PERL5LIB} ? $ENV{PERL5LIB} : ()), qw(lib)
+);
+
+my $connection = Object::Remote::Connector::Local->new->connect;
+
+my $ms = Object::Remote::ModuleSender->new(
+ dir_list => [ 't/lib' ]
+);
+
+my $ml = Object::Remote->new(
+ connection => $connection,
+ class => 'Object::Remote::ModuleLoader',
+ args => { module_sender => $ms },
+);
+
+my $proxy = Object::Remote->new(
+ connection => $connection,
+ class => 'ORTestClass'
+)->proxy;
+
+isnt($$, $proxy->pid, 'Different pid on the other side');
+
+is($proxy->counter, 0, 'Counter at 0');
+
+is($proxy->increment, 1, 'Increment to 1');
+
+is($proxy->counter, 1, 'Counter at 1');
+
+done_testing;