Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

Re: OO: sharing data across inheritance

by saskaqueer (Friar)
on Jun 02, 2004 at 10:02 UTC ( [id://359310]=note: print w/replies, xml ) Need Help??


in reply to OO: sharing data across inheritance

Perhaps you want something like what I've posted below? It keeps a copy of the options passed to the parent constructor and passes them, along with any new options, to the child constructor. I'm not sure if it's something like this you are looking for, or if I am way off base.

update: Added an iterator to the main script to show results of child creation. In a real-world example, you'd probably want methods to access the children, rather than digging around in the package internals as I have done below.

package Parent; use strict; use Child; sub new { my ($class, %opts) = @_; my $self = bless( { _opts => \%opts, # save parent options _children => [] # an array of children }, $class); return $self; } sub new_child { my ($self, %opts) = @_; my $child = Child->new( %{ $self->{_opts} }, # the saved parent options %opts # the new child options ); # save this child inside the parent object push @{ $self->{_children} }, $child; return $child; } 1; package Child; use strict; sub new { my ($class, %opts) = @_; my $self = bless( { _opts => \%opts }, $class ); return $self; } 1; package main; use strict; use Parent; # Create the parent object. We will create child objects # with it and save those child objects within the parent. my $p = Parent->new( foo => 1, bar => 2 ); # Create the first child. Pass an extra option in. my $c1 = $p->new_child( baz => 3 ); # Create a second child. Give it different options. my $c2 = $p->new_child( qux => 4, yeah => 5 ); # Iterate through children and see which options they got. for my $child ( @{ $p->{_children} } ) { while ( my ($k, $v) = each %{ $child->{_opts} } ) { print "$k ==> $v\n"; } print "\n"; }

Replies are listed 'Best First'.
Re: Re: OO: sharing data across inheritance
by skillet-thief (Friar) on Jun 02, 2004 at 11:35 UTC

    Wow, thanks for the lightning fast and enlightening response. This looks like what I was thinking about, but I have one more question.

    Is it necessary to store the children in the parent, like what you do in the Parent package:

    # save this child inside the parent object push @{ $self->{_children} }, $child;
    Or is it enough to pass the child a reference to the %opts?

    And thanks!

    s-t

      No, you do not need to save the children within the parent. I only did that as an example. If your project doesn't require that the parent keep track of the children it creates, then you can safely remove that section.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others cooling their heels in the Monastery: (6)
As of 2024-04-23 07:43 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found