package Object::Remote::ModuleLoader::Hook;
use Moo;
use Object::Remote::Logging qw( :log :dlog );
+ use Try::Tiny;
has sender => (is => 'ro', required => 1);
# unqualified INC forced into package main
sub Object::Remote::ModuleLoader::Hook::INC {
my ($self, $module) = @_;
log_debug { "Loading $module via " . ref($self) };
- if (my $code = $self->sender->source_for($module)) {
- open my $fh, '<', \$code;
- Dlog_trace { "Module sender successfully sent code for '$module': $code" } $code;
- return $fh;
+ try
+ {
+ if (my $code = $self->sender->source_for($module)) {
+ open my $fh, '<', \$code;
+ Dlog_trace { "Module sender successfully sent code for '$module': $code" } $code;
+ return $fh;
+ }
+ log_trace { "Module sender did not return code for '$module'" };
+ return;
+ }
+ catch
+ {
+ log_trace { "Module sender blew up - $_" };
+ if($_ =~ /Can't locate/)
+ {
+ # Fudge the error messge to make it work with
+ # Module::Runtime use_package_optimistically
+ # Module::Runtime wants - /\ACan't locate \Q$fn\E .+ at \Q@{[__FILE__]}\E line/
+ my ($package, $file, $line) = caller(9);
+ s/(in \@INC.)/$1 at $file line $line/;
+ }
+ die $_;
}
- log_trace { "Module sender did not return code for '$module'" };
- return;
}
}
my $remote = My::Data::TestModuleRuntime->new::on($connection);
is($remote->counter, 0, 'Counter at 0');
is($remote->increment, 1, 'Increment to 1');
- is($remote->has_missing_module, 0, 'Shouldn\'t have loaded module');
+ like exception {
+ my $o = $remote->create_object;
+ }, qr/Can't locate Not\/Found.pm in \@INC/, 'Should fail to load Not::Found';
+
}, undef, 'Checking Module::Runtime use_package_optimistically works correctly.';
done_testing;
sub increment { $_[0]->_set_counter($_[0]->counter + 1); }
-sub has_missing_module { HAS_MISSING_MODULE };
+sub create_object { use_package_optimistically('Not::Found')->new() };