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