in reply to Setting common object attributes
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;
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Setting common object attributes
by nevdka (Pilgrim) on Jul 18, 2013 at 23:09 UTC |