Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

Inheritance and Constants

by jgamble (Pilgrim)
on Aug 25, 2006 at 18:48 UTC ( #569673=perlquestion: print w/ replies, xml ) Need Help??
jgamble has asked for the wisdom of the Perl Monks concerning the following question:

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

Comment on Inheritance and Constants
Select or Download Code
Re: Inheritance and Constants
by ikegami (Pope) on Aug 25, 2006 at 19:12 UTC

    Subs are only inherited when called as (class or object) methods, as opposed to functions. The simplest solution would be to import them.

    Did you really need to post 100s of lines to show constants aren't inherited? How (Not) To Ask A Question

      Subs are only inherited when called as (class or object) methods, as opposed to functions.

      Thanks, I didn't know that.

      "Did you really need to post 100s of lines to show constants aren't inherited?"

      Well, given that I didn't know what aspect of my code was causing the problem, and that this is indeed a stripped-down version of it... I apologise for offending you.

      Hmm, I was going to comment on your => note, but I see you've already removed that comment. Never mind.

        You didn't offend me. It was a tip to get better service in the future.
Re: Inheritance and Constants
by ikegami (Pope) on Aug 25, 2006 at 19:53 UTC
    You gave value to your elements that assume the elements can be ORed together, but subs element and transmute assume otherwise. The following would remove this ambiguity.
    use constant Earth => 0; use constant Air => 1; use constant Fire => 2; use constant Water => 3; my @elmt_name = qw( Earth Air Fire Water ); # Gorgon::element return $elmt_name[ $self->{element} ]; # Gorgon::Euryale::transmute $elmt = ($elmt + 1) % 4; # Gorgon::Stheno::transmute $elmt = ($elmt - 1) % 4;

    Also, I find it funny that element accepts a number when used as a setter, but returns a string when used as a getter.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others avoiding work at the Monastery: (8)
As of 2014-08-21 06:51 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The best computer themed movie is:











    Results (128 votes), past polls