Hi everyone I hope you're doing fine, first I want to say this is crosspost: https://stackoverflow.com/questions/77557869/central-constructor-share-data-bless-hash-between-packages

I will tried to explain better here and find a solution to this problem, and I hope to learn something new.

I want to have access the latest data of parent constructor in all childs packages in $self.

why I want to do this:

In the constructor, I call external packages so I assigned the result to the hash reference constructor, inside child package I need that data and others config values of parent.

Possible solution:

I found a solution with rebless, and with this code, but I need to create variable and I have to call $self->{data}->{key} I want to avoid that . I wish I can call just like this $self->{key} inside child package without rebless.

Here a full working example, but have to call $self->{data}->{key}:

package Father; my $store_data = { }; sub new { my $class = shift; return bless { data => $store_data }, $class; } 1; package Daughter; use parent qw(Father); sub status { my $self = shift; $self->{data}->{news} = 'bad'; return $self; } sub other { my $self = shift; return $self; } 1; package Son; use parent qw(Father); sub set { my $self = shift; $self->{data}->{news} = 'good'; return $self; } 1; use Daughter; use Son; my $self = Daughter->new()->status; print Dumper($self); $self = Son->new()->set; print Dumper($self); $self = Daughter->new()->other; print Dumper($self); $VAR1 = bless( { 'data' => { 'news' => 'bad' } }, 'Daughter' ); $VAR1 = bless( { 'data' => { 'news' => 'good' } }, 'Son' ); $VAR1 = bless( { 'data' => { 'news' => 'good' } }, 'Daughter' );
do you have some suggestions ?

In reply to 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.