in reply to Share data bless hash of parent package with childs packages

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

Replies are listed 'Best First'.
Re^2: Share data bless hash of parent package with childs packages
by bliako (Abbot) on Dec 19, 2023 at 11:32 UTC
    duh! it seems i cleared my cookies before posting
      That's what "Theme Configuration" in Display Settings is for ;-)😉

      🔜🇽️

        I got the red theme OK but lately I have added an enhanced-contrast plugin to the browser and seems that the theme colors are changed.