Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

Moose: Giving objects access to data of class that created it

by nysus (Parson)
on May 25, 2017 at 19:28 UTC ( [id://1191240]=perlquestion: print w/replies, xml ) Need Help??

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

I'm curious to know how I might give objects created by a parent class access to the data in the parent class:

{ package parent 0.000001; use Moose; has 'changing_data' => (is => 'rw', isa => 'Str', default => ''); sub set_data { my $s = shift; $s->changing_data('foo foo foo'); } sub create_child { my $s = shift; my $data = shift; return child->new(changing_data => $data); } } { package child 0.00001; use Moose; extends 'parent'; } my $p = parent->new(); $p->set_data; my $child = $p->create_child($p->changing_data()); print $child->changing_data; $p->changing_data('bar bar bar'); print "\n"; print $child->changing_data; # desired output foo foo foo bar bar bar # actual output foo foo foo foo foo foo

$PM = "Perl Monk's";
$MCF = "Most Clueless Friar Abbot Bishop Pontiff Deacon Curate";
$nysus = $PM . ' ' . $MCF;
Click here if you love Perl Monks

Replies are listed 'Best First'.
Re: Moose: Giving objects access to data of class that created it
by tobyink (Canon) on May 25, 2017 at 20:25 UTC

    Is something like this what you want?

    use strict; use warnings; use feature 'say'; { package Tree; use Moose; has health_level => ( is => 'rw', isa => 'Int', ); sub grow_leaf { my $self = shift; Leaf->new(tree => $self); # pass $self on to child } } { package Leaf; use Moose; has tree => ( is => 'bare', handles => ['health_level'], ); } my $t = Tree->new(health_level => 100); my $l = $t->grow_leaf; say $l->health_level; # 100 $t->health_level(90); say $l->health_level; # 90

      Yup, that's what I ended up doing. It didn't dawn on me to do that. Thanks.

      $PM = "Perl Monk's";
      $MCF = "Most Clueless Friar Abbot Bishop Pontiff Deacon Curate";
      $nysus = $PM . ' ' . $MCF;
      Click here if you love Perl Monks

Re: Moose: Giving objects access to data of class that created it
by Anonymous Monk on May 25, 2017 at 19:51 UTC
    Code doesn't compile, what is changing_of_data?

    objects created by a parent class

    The parent class isn't creating any objects.

    Don't use lowercase names for classes, those are for pragmas like parent

      Code updated. Don't usually use lowercase names. Done for this one-off.

      $PM = "Perl Monk's";
      $MCF = "Most Clueless Friar Abbot Bishop Pontiff Deacon Curate";
      $nysus = $PM . ' ' . $MCF;
      Click here if you love Perl Monks

Re: Moose: Giving objects access to data of class that created it
by NetWallah (Canon) on May 26, 2017 at 19:08 UTC
    I got it to produce the desired results, after a few tweaks.
    Mainly, allow 'changing_data' to be a code-ref, and change child's calling syntax appropriately.
    use strict; use warnings; { package parent 0.000001; use Moose; has 'changing_data' => (is => 'rw', default => ''); sub set_data { my $s = shift; $s->changing_data('foo foo foo'); } sub create_child { my $s = shift; my $data = shift; return child->new(changing_data => $data); } } { package child 0.00001; use Moose; extends 'parent'; } my $p = parent->new(); $p->set_data; my $child = $p->create_child(sub{$p->changing_data()}); print $child->changing_data->(); $p->changing_data('bar bar bar'); print "\n"; print $child->changing_data->();

                    Once it hits the fan, the only rational choice is to sweep it up, package it, and sell it as fertilizer.

Re: Moose: Giving objects access to data of class that created it
by Anonymous Monk on May 25, 2017 at 20:23 UTC
    The child class only inherits the fact that it has a property named changing_data from the parent, each object only has one property named changing_data. An object is just a fancy hashref, you have two: $p = bless { changing_data=>'bar bar bar' }, 'parent' and $child = bless { changing_data=>'foo foo foo' }, 'child'. $p->changing_data('bar bar bar'); only sets the parent's property, how do you expect that to reach the child class?

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1191240]
Approved by kevbot
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others admiring the Monastery: (9)
As of 2024-04-18 11:44 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found