skillet-thief has asked for the wisdom of the Perl Monks concerning the following question:
I'm am still working on my comprehension of OO in Perl. I think I more or less grasp the general ideas but am having some trouble with the more practical applications.
Here is my current problem. I want to have a Parent class that has a new_child() method that blesses a new objet into the Child class that inherits from Parent. That should give me something like this, in the script that uses these modules:
#pseudocode, of course #!/usr/bin/perl use warnings; use strict; use Parent; my %data = ( param1 => "something", param2 => "something else"); my $p = Parent->new(\%data); my $c = $p->new_child("More data");
I would like to have param1, for example, be available to $c. Do I need to copy it explicitly in the constructor, by doing something like this in Parent.pm? (I know this would work, but I'm not sure that it is the best way to acheive what I want.)
Or is there some way for $c to refer to what is in $p?sub new_child { my $self = shift; my $more_data = shift; bless { param1 => $self->{param1}, more_data => $more_data }, "Child"; }
Or am I hopelessly off target on this whole thing?
Thanks and cheers,
s-t
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
•Re: OO: sharing data across inheritance
by merlyn (Sage) on Jun 02, 2004 at 10:05 UTC | |
by Abigail-II (Bishop) on Jun 03, 2004 at 22:54 UTC | |
|
Re: OO: sharing data across inheritance
by saskaqueer (Friar) on Jun 02, 2004 at 10:02 UTC | |
by skillet-thief (Friar) on Jun 02, 2004 at 11:35 UTC | |
by saskaqueer (Friar) on Jun 03, 2004 at 05:08 UTC | |
|
Re: OO: sharing data across inheritance
by adrianh (Chancellor) on Jun 02, 2004 at 10:54 UTC | |
by skillet-thief (Friar) on Jun 02, 2004 at 11:55 UTC | |
by adrianh (Chancellor) on Jun 02, 2004 at 13:39 UTC | |
|
Re: OO: sharing data across inheritance
by cyocum (Curate) on Jun 02, 2004 at 10:04 UTC | |
|
[followup] Re: OO: sharing data across inheritance
by skillet-thief (Friar) on Jun 03, 2004 at 12:52 UTC |