Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

Re: Perl Inheritance & module variables

by brian_d_foy (Abbot)
on Sep 20, 2008 at 19:41 UTC ( #712765=note: print w/ replies, xml ) Need Help??


in reply to Perl Inheritance & module variables

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


Comment on Re: Perl Inheritance & module variables
Select or Download Code

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (6)
As of 2014-12-27 00:13 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (176 votes), past polls