Really, really please jump on the Moose/Mouse/Moo bandwagon. Your Car class could be written as:
{ package Car; use Moo; use Types::Standard qw( Enum Int ); has drive_type => ( is => 'rw', isa => Enum['fwd', 'rwd', '4wd'], ); has body_type => ( is => 'rw', isa => Enum['sedan', 'wagon', 'coupe', 'hatch'], ); has engine_cap => ( is => 'rw', isa => Int, ); sub dump { require Data::Dumper; Data::Dumper->Dump(\@_); } } my $car = Car->new( drive_type => 'fwd', body_type => 'hatch', engine_cap => 1798, ); print $car->dump;
Isn't that pretty?
And subclassing becomes so easy that it's nothing to fear:
{ package Corolla; use Moo; extends 'Car'; has '+drive_type' => (default => 'fwd'); has '+body_type' => (default => 'hatch'); has '+engine_cap' => (default => 1798); } my $corolla = Corolla->new; print $corolla->dump;
In reply to Re: Setting common object attributes
by tobyink
in thread Setting common object attributes
by nevdka
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |