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

comment on

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

You're really asking how to write accessor methods. You call a method and you get the value of some property. You want to write methods that ask the question you want answered.

Here are two class, Parent and Child. The Child class inherits from Parent. Inside each class there is a VERSION method that returns the value of the $VERSION in that package. In neither case is $VERSION part of the object. They are class data.

package Parent; our $VERSION = 1.23; sub VERSION { $VERSION } sub child_version { $_[0]->VERSION } package Child; use base qw(Parent); our $VERSION = 5.43; sub VERSION { $VERSION } sub new { bless {}, $_[0]; } sub parent_version { $_[0]->SUPER::VERSION }

Those VERSION methods take care of answering the version question for each package as long as I call them directly as class methods:

print "Parent version is ", Parent->VERSION, "\n"; # 1.23 print "Child version is ", Child->VERSION, "\n"; # 5.43

But now I want to do that from a Child object. That's where the child_version and parent_version methods come in. Once I have the $child object, in parent_version I can get its parent version by calling its SUPER class. The SUPER is a virtual class that is the parent class of the one it is compiled in (Child in this case).

my $child = Child->new; print "Child version: ", $child->VERSION, "\n"; # 5.43 print "Parent version: ", $child->parent_version, "\n"; # 1.23

The VERSION and parent_version methods in Child take care of most everything you need. Where you're getting confused, I think, is how the Parent fits in to this. More on that in a moment. You can add a child_version method to Parent. It just takes the first thing on the argument list and calls VERSION on it. When I call child_version on the $child object, Perl doesn't find the child_version in Child so it looks in its superclass and finds it in Parent:

print "Child version: ", $child->child_version, "\n"; # 5.43

However, one of the points of inheritance is that you shouldn't need this two-way communication. You don't think about the parent class having part of it and the child class having another part. In this case, the child_version method is just the same thing as Child's VERSION:

print "Child version: ", $child->VERSION, "\n"; # 5.43 print "Child version: ", $child->child_version, "\n"; # 5.43

In Parent's child_version, it takes the first thing on the argument list, which is the $child object. That's how Parent accesses anything it needs to know in Child: just ask the child object that you got as the first argument.

--
brian d foy <brian@stonehenge.com>
Subscribe to The Perl Review

In reply to Re: Perl Inheritance & module variables by brian_d_foy
in thread Perl Inheritance & module variables by diabelek

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 pondering the Monastery: (9)
As of 2024-04-16 08:48 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found