0129b88ed8308eef57daaf2fa24d60eabe5bb99c
[scpubgit/Object-Remote.git] / lib / Object / Remote / ModuleSender.pm
1 package Object::Remote::ModuleSender;
2
3 use Config;
4 use File::Spec;
5 use List::Util qw(first);
6 use Moo;
7
8 has dir_list => (is => 'lazy');
9
10 sub _build_dir_list {
11   my %core = map +($_ => 1), @Config{qw(privlibexp archlibexp)};
12   [ grep !/$Config{archname}$/, grep !$core{$_}, @INC ];
13 }
14
15 sub source_for {
16   my ($self, $module) = @_;
17   if (my $find = Object::Remote::FromData->can('find_module')) {
18     if (my $source = $find->($module)) {
19       return $source;
20     }
21   }
22   my ($found) = first {  -f $_ }
23                   map File::Spec->catfile($_, $module),
24                     @{$self->dir_list};
25   die "Couldn't find ${module} in remote \@INC. dir_list contains:\n"
26       .join("\n", @{$self->dir_list})
27     unless $found;
28   open my $fh, '<', $found or die "Couldn't open ${found} for ${module}: $!";
29   return do { local $/; <$fh> };
30 }
31
32 1;