add Class::Load to dev prereqs
[scpubgit/Object-Remote.git] / lib / Object / Remote / ModuleSender.pm
CommitLineData
542d5b5c 1package Object::Remote::ModuleSender;
2
4a9fa1a5 3use Object::Remote::Logging qw( :log :dlog );
542d5b5c 4use Config;
5use File::Spec;
6use List::Util qw(first);
7use Moo;
8
9has dir_list => (is => 'lazy');
10
11sub _build_dir_list {
df8e0ca6 12 my %core = map +($_ => 1), grep $_, @Config{
13 qw(privlibexp archlibexp vendorarchexp sitearchexp)
14 };
4a9fa1a5 15 DlogS_trace { "dir list built in ModuleSender: $_" } [ grep !$core{$_}, @INC ];
542d5b5c 16}
17
18sub source_for {
19 my ($self, $module) = @_;
4a9fa1a5 20 log_debug { "locating source for module '$module'" };
7462160e 21 if (my $find = Object::Remote::FromData->can('find_module')) {
22 if (my $source = $find->($module)) {
b1f10b79 23 Dlog_trace { "source of '$module' was found by Object::Remote::FromData" };
624072a8 24 return $source;
7462160e 25 }
26 }
4a9fa1a5 27 log_trace { "Searching for module in library directories" };
542d5b5c 28 my ($found) = first { -f $_ }
29 map File::Spec->catfile($_, $module),
30 @{$self->dir_list};
7f5bfc4d 31 die "Can't locate ${module} in \@INC. (on remote host) dir_list contains:\n"
542d5b5c 32 .join("\n", @{$self->dir_list})
33 unless $found;
4a9fa1a5 34 log_debug { "found '$module' at '$found'" };
542d5b5c 35 open my $fh, '<', $found or die "Couldn't open ${found} for ${module}: $!";
36 return do { local $/; <$fh> };
37}
38
391;