First of all, if you are serious about this then you should create a working, minimal example of what you try to achieve. Preferably with test cases where it should fail and succeed.

Now to what I imagine you want to achieve with the help from the posts of the other monks here, I can see a few options:

First and simplest: use a state variable in Father to contain the data to be shared among parent and child classes. The drawback of this is that ALL instances of Father will share this data.

package Father; use strict; use warnings; use 5.010; sub new { my $class = shift; state $shared_data = {}; my $self = { 's' => $shared_data }; bless $self, $class; } sub s { return $_[0]->{s} } 1;
package Daughter; # or Son use strict; use warnings; use 5.010; use parent qw(Father); 1;
use strict; use warnings; use lib '.'; use Daughter; use Son; my $d = Daughter->new(); my $s = Son->new(); $d->s()->{'news'} = 'bad'; print $s->s()->{'news'}."\n"; my $d2 = Daughter->new(); print $d->s()->{'news'}."\n"; $d2->s()->{'news'} = 'bad123'; print $s->s()->{'news'}."\n"; print $d->s()->{'news'}."\n"; print $d2->s()->{'news'}."\n"; # prints bad bad bad123 bad123 bad123

The second option is to use the so-called factory method. Father is created with caller-supplied shared data. Then Daughters and Sons are still separate classes but their instantiation should be ONLY via the Father (you can change their constructor name to be _new and pretend that nobody will notice, in proper OO this is achieved with private constructors etc.) All such subclassed objects will share the same data:

my $shared_data = {}; my $father = Father->new($shared_data); my $d = $father->new_daughter(); ...

The benefit of this approach is that only children of a specific father will share data. So you can have multiple unhappy families as opposed to a single happy family as per Tolstoy's insigtful observation: Happy families are all alike; every unhappy family is unhappy in its own way.

I leave the implementation to you. And please put more effort if you want help.

bw, bliako


In reply to Re: Share data bless hash of parent package with childs packages by Anonymous Monk
in thread Share data bless hash of parent package with childs packages by Thenothing

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.