Let's say that I have a module, Gorgon.pm, that uses inheritance.

package Gorgon; use 5.6.1; use Exporter qw(import); use strict; use warnings; use vars qw($VERSION); $VERSION = '1.00'; our($Earth, $Air, $Fire, $Water)= (0x0001, 0x0002, 0x0004, 0x0008); our %elmt_name = ( ($Earth, "Earth"), ($Air, "Air"), ($Fire, "Fire"), ($Water, "Water"), ); sub new { my $class = shift; my $self = {}; $class = "Gorgon::Euryale" if ($class eq "Gorgon"); bless($self, $class); return $self->_setup(); } sub element { my $self = shift; $self->{element} = $_ if (scalar @_ > 0); return $elmt_name{ $self->{element} }; } package Gorgon::Euryale; use strict; use warnings; use base qw(Gorgon); sub _setup { my $self = shift; $self->{element} = $Water; return $self } sub transmute { my $self = shift; my $elmt = $self->{element}; if ($elmt == $Water ) {$elmt = $Earth ;} else {$elmt <<= 1;} $self->{element} = $elmt; return $elmt; } package Gorgon::Stheno; use strict; use warnings; use base qw(Gorgon); sub _setup { my $self = shift; $self->{element} = $Earth; return $self } sub transmute { my $self = shift; my $elmt = $self->{element}; if ($elmt == $Earth) {$elmt = $Water;} else {$elmt >>= 1;} $self->{element} = $elmt; return $elmt; } 1;

The gorg.pl script that uses it is:

#/usr/local/perl use Gorgon; use strict; use warnings; my $walk = Gorgon::Stheno->new(); die "Something bad happened $!" unless ($walk); for (1..8) { $walk->transmute(); print $walk->element(), "\n"; } exit(0);

It all works as designed, and everyone is happy. But one day it's decided to change the global 'our' variables to constants. A simple change:

package Gorgon; use 5.6.1; use Exporter qw(import); use strict; no strict qw(subs); use warnings; use vars qw($VERSION); $VERSION = '2.00'; use constant Earth => 0x0001; use constant Air => 0x0002; use constant Fire => 0x0004; use constant Water => 0x0008; our %elmt_name = ( (Earth, "Earth"), (Air, "Air"), (Fire, "Fire"), (Water, "Water"), ); sub new { my $class = shift; my $self = {}; $class = "Gorgon::Euryale" if ($class eq "Gorgon"); bless($self, $class); return $self->_setup(); } sub element { my $self = shift; $self->{element} = $_ if (scalar @_ > 0); return $elmt_name{ $self->{element} }; } package Gorgon::Euryale; use strict; no strict qw(subs); use warnings; use base qw(Gorgon); sub _setup { my $self = shift; $self->{element} = Water; return $self } sub transmute { my $self = shift; my $elmt = $self->{element}; if ($elmt == Water ) {$elmt = Earth ;} else {$elmt <<= 1;} $self->{element} = $elmt; return $elmt; } package Gorgon::Stheno; use strict; no strict qw(subs); use warnings; use base qw(Gorgon); sub _setup { my $self = shift; $self->{element} = Earth; return $self } sub transmute { my $self = shift; my $elmt = $self->{element}; if ($elmt == Earth) {$elmt = Water;} else {$elmt >>= 1;} $self->{element} = $elmt; return $elmt; } 1;

Note that Version 2.00 has a "no strict qw(subs);" line, which I had to put in, but which may be covering up a problem that I'm unaware of.

In any event, where I am using the constants in the inherited packages, I get errors like "Argument "Water" isn't numeric in numeric eq (==) at Gorgon.pm line 82." I do not get this error message in the Gorgon package itself where %elmt_name is created using the constants.

Now constants are in-lined functions, and I would have thought that as functions they would be inherited along with the other functions in the Gorgon.pm package, but obviously I'm wrong there. What is (or are) the mistake(s) in my reasoning, and how do I get around this?

Thanks,
-john


In reply to Inheritance and Constants by jgamble

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



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.