start adding logs and add support for routed logs and logging to stderr
[scpubgit/Object-Remote.git] / lib / Object / Remote / ModuleSender.pm
1 package Object::Remote::ModuleSender;
2
3 use Object::Remote::Logging qw( :log :dlog );
4 use Config;
5 use File::Spec;
6 use List::Util qw(first);
7 use Moo;
8
9 has dir_list => (is => 'lazy');
10
11 sub _build_dir_list {
12   my %core = map +($_ => 1), grep $_, @Config{
13     qw(privlibexp archlibexp vendorarchexp sitearchexp)
14   };
15   DlogS_trace { "dir list built in ModuleSender: $_" } [ grep !$core{$_}, @INC ];
16 }
17
18 sub source_for {
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       return Dlog_trace { "Object::Remote::FromData->find_module('$module') returned '$_'" } $source;
24     }
25   }
26   log_trace { "Searching for module in library directories" };
27   my ($found) = first {  -f $_ }
28                   map File::Spec->catfile($_, $module),
29                     @{$self->dir_list};
30   die "Couldn't find ${module} in remote \@INC. dir_list contains:\n"
31       .join("\n", @{$self->dir_list})
32     unless $found;
33   log_debug { "found '$module' at '$found'" };
34   open my $fh, '<', $found or die "Couldn't open ${found} for ${module}: $!";
35   return do { local $/; <$fh> };
36 }
37
38 1;