Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

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.)

sub new_child { my $self = shift; my $more_data = shift; bless { param1 => $self->{param1}, more_data => $more_data }, "Child"; }
Or is there some way for $c to refer to what is in $p?

Or am I hopelessly off target on this whole thing?

Thanks and cheers,

s-t


In reply to OO: sharing data across inheritance by skillet-thief

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

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

    No recent polls found