habit_forming has asked for the wisdom of the Perl Monks concerning the following question:

Oh great monks! Hear me!

Is there a way to have Perl create an object and stuff it into shared memory such that another Perl process could come up and use (a.k.a read, write, delete) that object just as if it had new'ed that object itself?

Is this possible? Does anyone have an idea of where I might start?

Cheers!
--habit

Replies are listed 'Best First'.
Re: shared memory and objects
by perrin (Chancellor) on Oct 25, 2004 at 19:02 UTC
    The short answer is no. The longer answer is that you can freeze an object with Storable, put it in a file or shared memory, and then thaw it in the other process and use it. The effect is the same, provided your objects are serializable. That's how things like IPC::Shareable work.

      Or communicate it thru Socket:

      Client: use Storable qw(freeze); use IO::Socket; use strict; use warnings; my $c = new IO::Socket::INET(Proto => "udp", PeerAddr => "localhost", PeerPort => 3000, Timeout => 1); print "Connected\n"; my $h = {"key" => [1,2,3,4,5,6,7,8,9,10]}; my $arr_freezed = freeze($h); $c->send($arr_freezed); close $c; Server: use IO::Socket; use Storable qw(thaw); use Data::Dumper; use strict; use warnings; my $server = new IO::Socket::INET(Timeout => 7200, Proto => "udp", LocalPort => 3000, LocalHost => "localhost"); print "Server is listening ...\n"; my $arr_freezed; $server->recv($arr_freezed, 1000); my $h = thaw($arr_freezed); print Dumper($h); close $server;

        Modified above example a little bit, so now it sends a freezed object over:

        blah.pm: package blah; sub new { $self = {}; bless($self); return $self; } sub setA { my ($self, $a) = @_; $self->{"A"} = $a; } sub getA { my $self = shift; return $self->{"A"} } 1; s.pl: use IO::Socket; use blah; use Storable qw(thaw); use Data::Dumper; use strict; use warnings; my $server = new IO::Socket::INET(Timeout => 7200, Proto => "udp", LocalPort => 3000, LocalHost => "localhost"); print "Server is listening ...\n"; my $blah_freezed; $server->recv($blah_freezed, 1000); my $blah = thaw($blah_freezed); print $blah->getA(); close $server; c.pl: use Storable qw(freeze); use blah; use IO::Socket; use strict; use warnings; my $c = new IO::Socket::INET(Proto => "udp", PeerAddr => "localhost", PeerPort => 3000, Timeout => 1); print "Connected\n"; my $blah = blah->new(); $blah->setA(123); my $blah_freezed = freeze($blah); $c->send($blah_freezed); close $c;
Re: shared memory and objects
by ides (Deacon) on Oct 25, 2004 at 21:19 UTC
    Check out Cache::Memcached on CPAN and the main project at http://www.danga.com/memcached. It will do all that you require and then some. For example, it will allow you to share objects across multiple servers which you can't do with shared memory.

    Frank Wiles <frank@wiles.org>
    http://www.wiles.org

      Cache::Memcached does exactly what I descibed above: it serializes objects using Storable and sends them to a server. If you don't need to share data across a cluster of machines, BerkeleyDB and Cache::FastMmap both offer better performance than Cache:Memcached.
        Perrin's right, those are faster options than Cache::Memcached. It was just the first option that came to mind when I read the question.

        Frank Wiles <frank@wiles.org>
        http://www.wiles.org

Re: shared memory and objects
by dragonchild (Archbishop) on Oct 25, 2004 at 20:09 UTC
    mod_perl does something similar - you might want to look at that.

    Also, you might want to look at threading your application. It's basically multi-process, but data is shared by default, not unshared by default. Of course, there's a host of new problems that goes along with that, but if you want to ...

    Another option is to use SOAP or XML-RPC or some other client/server option. I do that with XML-RPC and it works out really great.

    Being right, does not endow the right to be rude; politeness costs nothing.
    Being unknowing, is not the same as being stupid.
    Expressing a contrary opinion, whether to the individual or the group, is more often a sign of deeper thought than of cantankerous belligerence.
    Do not mistake your goals as the only goals; your opinion as the only opinion; your confidence as correctness. Saying you know better is not the same as explaining you know better.

      mod_perl dooes not do something similar. There is no read/write sharing of data. Threads do not share objects either, although you can sort of work around this by blessing the data structures in every thread.