Hello marioroy,

Thanks for your excellent reply. Besides giving me a couple of very nice examples you also explained sharing to me and the importance of it. I start to see now that I definitively need to do some more studying regards this subject.

One of your answers (in italic below) leads me to another question regards sharing nested objects

>> "The tie statement does not deeply share key-values during construction. The way to shared nested hash/array structures is explicitly via the STORE method"

In case objects are shared that do not have STORE and FETCH methods, and they have nested methods and objects, does this mean that the methods cannot be reached and the objects don't get to be shared?

Example, Bar object has nested Foo object (inside hash key nestedFoo):

Foo and Bar definitions:

BEGIN { package Foo ; use strict ; use warnings ; sub new { my $class = shift ; my $this = { _var => 1, } ; bless $this, $class ; } sub task { # Long operation task Foo ~ 2 seconds my $this = shift ; print "Starting task Foo for $_[0]\n" ; sleep(2) ; ++$this->{ _var } ; print "Finished task Foo for $_[0]\n" ; } package Bar ; use strict ; use warnings ; sub new { my $class = shift ; # This is not very realistic code. # here it is just used as an # example to create an object that # contains a nested object my $this = { _var => 1, } ; $this->{ nestedFoo } = $_[0] ; bless $this, $class ; } sub task { # Long operation task Bar ~ 6 seconds my $this = shift ; print "Starting task Bar for $_[0]\n" ; sleep(6) ; ++$this->{ _var } ; print "Finished task Bar for $_[0]\n" ; } }

Main:

package main ; use strict ; use warnings ; use MCE::Shared ; use Data::Dumper ; my $foo = new Foo ; my $bar = new Bar( $foo ) ; # my $barShared = MCE::Shared->share( { module => 'Bar' }, $foo ) ; # or # from examples: my $ob = MCE::Shared->share( $blessed_object ); my $barSh = MCE::Shared->share( Bar->new( $foo ) ) ; print Dumper( $barSh->export ) . "\n" ; $barSh->task( 'Bar' ) ; # OK $barSh->{ nestedFoo }->task( 'Foo' ) ; # Not a HASH reference # Program died here $barSh->export->{ nestedFoo }->task( 'Foo' ) ; # OK, but not shared? __END__ $VAR1 = bless( { '_var' => 1, 'nestedFoo' => bless( { '_var' => 1 }, 'Foo' ) }, 'Bar' ); Starting task Bar for Bar Not a HASH reference at testHobo3.pl line 60, <__ANONIO__> line 1. Finished task Bar for Bar

Is there a way to 'get' the nestedFoo in shared context?

Is there a way to execute task for Foo without export?

edit: I forgot to mention the nested _var in each of the objects. What I mean with 'shared context' is that each _var become 2 inside of the share after the task methods have been executed.


In reply to Re^2: Hobo with a bit of recursion by Veltro
in thread Hobo with a bit of recursion by Veltro

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.