package Car; use strict; use warnings; use Carp; sub new { my ($class) = @_; my $self = {}; bless($self, $class); return $self; } sub drive_type { my ($self, $drive) = @_; if ($drive) { unless ($drive =~ /^(?:fwd|rwd|4wd)$/) { croak "$drive not a valid drive type. Use fwd, rwd or 4wd\n"; } $self->{'drive'} = $drive; } return $self->{'drive'}; } sub body_type { my ($self, $body) = @_; if ($body) { unless ($body =~ /^(?:sedan|wagon|coupe|hatch)$/) { croak "$body not a valid body type. Use sedan, wagon, coupe or hatch\n"; } $self->{'body'} = $body; } return $self->{'body'}; } sub engine_cap { my ($self, $cap) = @_; if ($cap) { $self->{'engine_cap'} = $cap; } return $self->{'engine_cap'}; } 1; #### my $car = Car->new(); $car->drive_type('fwd'); $car->body_type('hatch'); $car->engine_cap('1798');