+ - allow loading of classes and packages from __DATA__
- allow transfer of scalar references
0.001001 - 2012-07-12
--- /dev/null
+package Object::Remote::FromData;
+
+use strictures 1;
+use Object::Remote;
+
+our %Modules;
+our %Not_Loaded_Yet;
+our %Seen;
+
+sub import {
+ my $target = caller;
+ return if $Seen{$target};
+ $Seen{$target} = $Not_Loaded_Yet{$target} = 1;
+}
+
+sub flush_loaded {
+ foreach my $key (keys %Not_Loaded_Yet) {
+ my $data_fh = do { no strict 'refs'; *{"${key}::DATA"} };
+ my $data = do { local $/; <$data_fh> };
+ my %modules = reverse(
+ $data =~ m/(^package ([^;]+);\n.*?(?:(?=^package)|\Z))/msg
+ );
+ $_ .= "\n1;\n" for values %modules;
+ @Modules{keys %modules} = values %modules;
+ delete $Not_Loaded_Yet{$key};
+ }
+}
+
+sub find_module {
+ flush_loaded;
+ my ($module) = @_;
+ $module =~ s/\//::/g;
+ $module =~ s/\.pm$//;
+ return $Modules{$module};
+}
+
+1;
sub source_for {
my ($self, $module) = @_;
+ if (my $find = Object::Remote::FromData->can('find_module')) {
+ if (my $source = $find->($module)) {
+ return $source;
+ }
+ }
my ($found) = first { -f $_ }
map File::Spec->catfile($_, $module),
@{$self->dir_list};
--- /dev/null
+use strictures 1;
+use Test::More;
+use Sys::Hostname qw(hostname);
+
+use Object::Remote::FromData;
+
+my $connection = Object::Remote->connect('-');
+
+my $remote = My::Data::TestClass->new::on($connection);
+
+is($remote->counter, 0, 'Counter at 0');
+
+is($remote->increment, 1, 'Increment to 1');
+
+is($remote->counter, 1, 'Counter at 1');
+
+is(
+ My::Data::TestPackage->can::on($connection, 'hostname')->(),
+ hostname(),
+ 'Remote sub call ok'
+);
+
+done_testing;
+
+__DATA__
+package My::Data::TestClass;
+
+use Moo;
+
+has counter => (is => 'rwp', default => sub { 0 });
+
+sub increment { $_[0]->_set_counter($_[0]->counter + 1); }
+
+package My::Data::TestPackage;
+
+use Sys::Hostname;