Inheri-tance, actually.. you had a typo. ;-)
By my standards, there's exactly one reason to use inheritance: to define the shared interface and default behavior for a family of similar classes.
The code you gave doesn't quite do that. The default interface for Vehicle is 'pretty much anything'. You've tried to roll your own way of making the interfaces stable, but it doesn't work as well as it could. There's no guarantee Bike and Car will share the same data members (and, by extension, the same access methods), for instance. You've forced the interfaces to be the same by defining the '%valid' hash redundantly, but you'd be better off sinking that information down to the Vehicle class.
In fact, I'd suggest getting rid of AUTOLOAD entirely, defining the methods you want explicitly in Vehicle, then scrapping the '%ro' hash and overriding the subclass methods to get the behavior you want:
package Vehicle;
sub new {
my ($type, $data) = @_;
my $O = bless $type->_defaults(), $type; # [1]
for $k (keys %$O) {
$O->{$k} = $data->{$k} if defined ($data->{$k}); # [2]
}
$O->_sanity_check(); # [3]
return $O;
}
=item new (hash-ref: data) : Vehicle-ref
[1] We start from a prototype data structure known to be good.
[2] Now we override any values defined by the arguments. We
only override values that are already in the prototype,
though. We don't want to add anomalous data members by
just copying everything straight over.
[3] Now we run the new values through a hygiene filter to make
sure everything's still good.
=cut
sub _defaults {
return ({
'wheels' => 0,
'doors' => 0,
'color' => 'none',
'passengers' => 0,
});
}
=item _defaults (nil) : hash-ref
This method takes no input, and returns a pre-filled hash of valid
attributes for a given vehicle type.
Technically, this is a lobotomized version of the Factory Method
design pattern.
=cut
sub _sanity_check {
my $O = shift;
if ($O->{'wheels'}) {
print STDERR "I ran into a problem.. "
. "a generic vehicle shouldn't have "
. $O->{'wheels'} . ' '
. "wheels.\n"
;
}
if ($O->{'doors'}) {
print STDERR "I ran into a problem.. "
. "a generic vehicle shouldn't have "
. $O->{'doors'} . ' '
. "doors.\n"
;
}
if ('none' ne $O->{'color'}) {
print STDERR "I ran into a problem.. "
. "a generic vehicle shouldn't be colored "
. $O->{'color'}
. ".\n"
;
}
if ($O->{'passengers'}) {
print STDERR "I ran into a problem.. "
. "a generic vehicle doesn't carry "
. $O->{'passengers'} . ' '
. "passengers.\n"
;
}
return;
}
=item _sanity_check (nil) : nil
This method doesn't take any input or return any value as
output, but it does print any errors it sees to STDERR. In
a real program, we'd use some kind of trace to see when and
where the error occured.
=cut
sub _access {
my ($O, $item, $value) = @_;
if (defined ($value)) {
$O->{$item} = $value;
$O->_sanity_check();
}
return ($O->{$item});
}
=item _access (item, value) : value
This is a generic back-end for the accessor functions. It
takes the attribute name and an optional value as input, and
returns the item's value as output.
I've thrown in a sanity check every time an item's value is
changed, just for the sake of paranoia.
=cut
sub Wheels { return ($_[0]->_access ('wheels', $_[1])); }
sub Doors { return ($_[0]->_access ('doors', $_[1])); }
sub Color { return ($_[0]->_access ('color', $_[1])); }
sub Passengers { return ($_[0]->_access ('passengers', $_[1])); }
=item accessor methods
These are trivial methods that handle get-and-set operations
for the attributes. The fact that _access() does an
automatic sanity check after setting any new value means we
don't have to put sanity checks in each of these methods..
though we probably would do individual sanity checks in a
real application.
This is one of those cases where 'lazy' means 'doing lots
of work now so we won't have to do even more work later'.
=cut
package Bike;
@ISA = qw( Vehicle );
=item Class Bike
This class will override _defaults(), _sanity_check(), and
possibly some of the access methods if we want to make
'wheels' always equal 2, for instance:
sub Wheels {
if ((defined $_[1]) && (2 != $_[1])) {
print "You can't do that. I won't let you. So there.\n";
}
}
=cut
package Car;
@ISA = qw( Vehicle );
=item Class Car
Again, this class will override _defaults(),
_sanity_check(), and any access methods we want to harden
against changes.
=cut