1 package Object::Remote::ModuleSender;
3 use Object::Remote::Logging qw( :log :dlog );
6 use List::Util qw(first);
9 has dir_list => (is => 'lazy');
12 my %core = map +($_ => 1), grep $_, @Config{
13 qw(privlibexp archlibexp vendorarchexp sitearchexp)
15 DlogS_trace { "dir list built in ModuleSender: $_" } [ grep !$core{$_}, @INC ];
19 my ($self, $module) = @_;
20 log_debug { "locating source for module '$module'" };
21 if (my $find = Object::Remote::FromData->can('find_module')) {
22 if (my $source = $find->($module)) {
23 Dlog_trace { "source of '$module' was found by Object::Remote::FromData" };
27 log_trace { "Searching for module in library directories" };
29 for my $inc (@{$self->dir_list}) {
31 my $full_module = File::Spec->catfile($inc, $module);
32 next unless -f $full_module;
33 log_debug { "found '$module' at '$found'" };
34 open my $fh, '<', $full_module or die "Couldn't open ${full_module} for ${module}: $!";
35 return do { local $/; <$fh> };
38 my $data = _read_dynamic_inc($inc, $module);
43 die "Couldn't find ${module} in remote \@INC. dir_list contains:\n"
44 .join("\n", @{$self->dir_list});
47 sub _read_dynamic_inc {
48 my ($inc, $module) = @_;
50 my ($fh, $cb, $state);
51 if (ref $inc eq 'CODE') {
52 ($fh, $cb, $state) = $inc->($inc, $module);
54 elsif (ref $inc eq 'ARRAY') {
55 ($fh, $cb, $state) = $inc->[0]->($inc, $module);
57 elsif ($inc->can('INC')) {
58 ($fh, $cb, $state) = $inc->INC($module);
66 my $res = $cb->($cb, $state);
76 my $res = $cb->($cb, $state);
83 return do { local $/; <$fh> };