X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?p=scpubgit%2FObject-Remote.git;a=blobdiff_plain;f=lib%2FObject%2FRemote.pm;h=a20ef974f1fbb6ac9486ddfa05895e25b02fed81;hp=47f2906e088cc1806353c7f3431f17df8df70170;hb=df8e0ca6a336fb37fd14d40fa345e787bc75b8c2;hpb=676438a11cbf6bd49102369824c9d87f70964fd3 diff --git a/lib/Object/Remote.pm b/lib/Object/Remote.pm index 47f2906..a20ef97 100644 --- a/lib/Object/Remote.pm +++ b/lib/Object/Remote.pm @@ -2,14 +2,20 @@ package Object::Remote; use Object::Remote::MiniLoop; use Object::Remote::Handle; +use Module::Runtime qw(use_module); + +our $VERSION = '0.002002'; # 0.2.2 sub new::on { my ($class, $on, @args) = @_; - Object::Remote::Handle->new( - connection => $on, - class => $class, - args => \@args - )->proxy; + my $conn = __PACKAGE__->connect($on); + return $conn->remote_object(class => $class, args => \@args); +} + +sub can::on { + my ($class, $on, $name) = @_; + my $conn = __PACKAGE__->connect($on); + return $conn->remote_sub(join('::', $class, $name)); } sub new { @@ -17,8 +23,134 @@ sub new { Object::Remote::Handle->new(@_)->proxy; } +sub connect { + my ($class, $to) = @_; + use_module('Object::Remote::Connection')->maybe::start::new_from_spec($to); +} + sub current_loop { our $Current_Loop ||= Object::Remote::MiniLoop->new } 1; + +=head1 NAME + +Object::Remote - Call methods on objects in other processes or on other hosts + +=head1 SYNOPSIS + +Creating a connection: + + use Object::Remote; + + my $conn = Object::Remote->connect('myserver'); # invokes ssh + +Calling a subroutine: + + my $capture = IPC::System::Simple->can::on($conn, 'capture'); + + warn $capture->('uptime'); + +Using an object: + + my $eval = Eval::WithLexicals->new::on($conn); + + $eval->eval(q{my $x = `uptime`}); + + warn $eval->eval(q{$x}); + +Importantly: 'myserver' only requires perl 5.8+ - no non-core modules need to +be installed on the far side, Object::Remote takes care of it for you! + +=head1 DESCRIPTION + +Object::Remote allows you to create an object in another process - usually +one running on another machine you can connect to via ssh, although there +are other connection mechanisms available. + +The idea here is that in many cases one wants to be able to run a piece of +code on another machine, or perhaps many other machines - but without having +to install anything on the far side. + +=head1 COMPONENTS + +=head2 Object::Remote + +The "main" API, which provides the L method to create a connection +to a remote process/host, L to create an object on a connection, +and L to retrieve a subref over a connection. + +=head2 Object::Remote::Connection + +The object representing a connection, which provides the +L and +L methods that are used by +L and L to return proxies for objects and subroutines +on the far side. + +=head2 Object::Remote::Future + +Code for dealing with asynchronous operations, which provides the +L syntax for calling a possibly +asynchronous method without blocking, and +L and L +to block until an asynchronous call completes or fails. + +=head1 METHODS + +=head2 connect + + my $conn = Object::Remote->connect('-'); # fork()ed connection + + my $conn = Object::Remote->connect('myserver'); # connection over ssh + + my $conn = Object::Remote->connect('user@myserver'); # connection over ssh + + my $conn = Object::Remote->connect('root@'); # connection over sudo + +=head2 new::on + + my $eval = Eval::WithLexicals->new::on($conn); + + my $eval = Eval::WithLexicals->new::on('myserver'); # implicit connect + + my $obj = Some::Class->new::on($conn, %args); # with constructor arguments + +=head2 can::on + + my $hostname = Sys::Hostname->can::on($conn, 'hostname'); + + my $hostname = Sys::Hostname->can::on('myserver', 'hostname'); + +=head1 SUPPORT + +IRC: #web-simple on irc.perl.org + +=head1 AUTHOR + +mst - Matt S. Trout (cpan:MSTROUT) + +=head1 CONTRIBUTORS + +phaylon - Robert Sedlacek (cpan:PHAYLON) + +=head1 SPONSORS + +Parts of this code were paid for by + + Socialflow L + + Shadowcat Systems L + +=head1 COPYRIGHT + +Copyright (c) 2012 the Object::Remote L, L and +L as listed above. + +=head1 LICENSE + +This library is free software and may be distributed under the same terms +as perl itself. + +=cut