in reply to Hash from package
This and your previous question are both really about OO design. Think about what should be part of the interface for the base class and provide support for that. For example, if you want to have an object describe itself you could do something like:
use strict; use warnings; package Vehicle; sub new { my $class = shift; # Provide some defaults my $self = {wheels => 4, engine => 1800, doors => 4}; return bless $self, $class; } sub describe { my $self = shift; return <<"desc"; My engine is $self->{engine}cc. I have $self->{wheels} wheels and $self->{doors} doors. desc } package Car; our @ISA = ('Vehicle'); sub new { my $class = shift; my $self = new Vehicle ($class); $self->{colour} = 'blue'; return bless $self, $class; } sub describe { my $self = shift; my $str = $self->SUPER::describe (); return $str . <<"desc"; My colour is $self->{colour}. desc } package main; my $car = new Car; print $car->describe ();
Prints:
My engine is 1800cc. I have 4 wheels and 4 doors. My colour is blue.
Although actually that is still pretty nasty. Consider what would happen if you needed a bike: no engine and no doors, or even worse a monocycle with only one wheel ("I have 1 wheels and 0 doors.").
A good starting point for some of this stuff is perlboot, but take a look around the Tutorials area too.
|
|---|