<?xml version="1.0" encoding="windows-1252"?>
<node id="712765" title="Re: Perl Inheritance &amp; module variables" created="2008-09-20 15:41:04" updated="2008-09-20 11:41:04">
<type id="11">
note</type>
<author id="366986">
brian_d_foy</author>
<data>
<field name="doctext">
&lt;p&gt;
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.
&lt;/p&gt;
&lt;p&gt;
Here are two class, &lt;code&gt;Parent&lt;/code&gt; and &lt;code&gt;Child&lt;/code&gt;. The &lt;code&gt;Child&lt;/code&gt; class inherits from &lt;code&gt;Parent&lt;/code&gt;. Inside each class there is a &lt;code&gt;VERSION&lt;/code&gt; method that returns the value of the &lt;code&gt;$VERSION&lt;/code&gt; in that package. In neither case is &lt;code&gt;$VERSION&lt;/code&gt; part of the object. They are class data.
&lt;/p&gt;
&lt;code&gt;
package Parent;

our $VERSION = 1.23;

sub VERSION { $VERSION }

sub child_version { $_[0]-&gt;VERSION }

package Child;
use base qw(Parent);

our $VERSION = 5.43;

sub VERSION { $VERSION }

sub new { bless {}, $_[0]; }

sub parent_version { $_[0]-&gt;SUPER::VERSION }
&lt;/code&gt;
&lt;p&gt;
Those &lt;code&gt;VERSION&lt;/code&gt; methods take care of answering the version question for each package as long as I call them directly as class methods:
&lt;/p&gt;
&lt;code&gt;
print "Parent version is ", Parent-&gt;VERSION, "\n"; # 1.23

print "Child version is ", Child-&gt;VERSION, "\n";  # 5.43
&lt;/code&gt;
&lt;p&gt;
But now I want to do that from a &lt;code&gt;Child&lt;/code&gt; object. That's where the &lt;code&gt;child_version&lt;/code&gt; and &lt;code&gt;parent_version&lt;/code&gt; methods come in. Once I have the &lt;code&gt;$child&lt;/code&gt; object, in &lt;code&gt;parent_version&lt;/code&gt;  I can get its parent version by calling its &lt;code&gt;SUPER&lt;/code&gt; class. The &lt;code&gt;SUPER&lt;/code&gt; is a virtual class that is the parent class of the one it is compiled in (&lt;code&gt;Child&lt;/code&gt; in this case).
&lt;/p&gt;
&lt;code&gt;
my $child = Child-&gt;new;

print "Child version: ",  $child-&gt;VERSION, "\n";            # 5.43
print "Parent version: ", $child-&gt;parent_version, "\n"; # 1.23
&lt;/code&gt;
&lt;p&gt;
The &lt;code&gt;VERSION&lt;/code&gt; and &lt;code&gt;parent_version&lt;/code&gt; methods in &lt;code&gt;Child&lt;/code&gt; take care of most everything you need. Where you're getting confused, I think, is how the &lt;code&gt;Parent&lt;/code&gt; fits in to this. More on that in a moment. You can add a &lt;code&gt;child_version&lt;/code&gt; method to &lt;code&gt;Parent&lt;/code&gt;. It just takes the first thing on the argument list and calls &lt;code&gt;VERSION&lt;/code&gt; on it. When I call &lt;code&gt;child_version&lt;/code&gt; on the &lt;code&gt;$child&lt;/code&gt; object, Perl doesn't find the &lt;code&gt;child_version&lt;/code&gt; in &lt;code&gt;Child&lt;/code&gt; so it looks in its superclass and finds it in &lt;code&gt;Parent&lt;/code&gt;:
&lt;/p&gt;
&lt;code&gt;
print "Child version: ",  $child-&gt;child_version, "\n"; # 5.43
&lt;/code&gt;
&lt;p&gt;
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 &lt;code&gt;child_version&lt;/code&gt; method is just the same thing as &lt;code&gt;Child&lt;/code&gt;'s &lt;code&gt;VERSION&lt;/code&gt;:
&lt;/p&gt;
&lt;code&gt;
print "Child version: ",  $child-&gt;VERSION, "\n";          # 5.43
print "Child version: ",  $child-&gt;child_version, "\n"; # 5.43
&lt;/code&gt;
&lt;p&gt;
In &lt;code&gt;Parent&lt;/code&gt;'s &lt;code&gt;child_version&lt;/code&gt;, it takes the first thing on the argument list, which is the &lt;code&gt;$child&lt;/code&gt; object. That's how &lt;code&gt;Parent&lt;/code&gt; accesses anything it needs to know in &lt;code&gt;Child&lt;/code&gt;: just ask the child object that you got as the first argument.
&lt;/p&gt;
&lt;!-- Node text goes above. Div tags should contain sig only --&gt;
&lt;div class="pmsig"&gt;&lt;div class="pmsig-366986"&gt;
-- &lt;br /&gt;
brian d foy &lt;brian@stonehenge.com&gt;&lt;br /&gt;
Subscribe to &lt;a href="http://www.theperlreview.com"&gt;The Perl Review&lt;/a&gt;
&lt;/div&gt;&lt;/div&gt;</field>
<field name="root_node">
712303</field>
<field name="parent_node">
712303</field>
</data>
</node>
