Hi nevdka,
..But that means having a subclass, which is what I want to avoid. Is there a better way to set common attributes?..
As it has previously been said, doing a subclass of the class "Car" would have been the way to go, however, if the only thing that changes is the "type" of car, i.e Toyota, kia, Ford are what is changing then one can just provision that into a generic class of Car like so:

use warnings; use strict; { # begin of Car class package Car; sub new { my $class = shift; my $self = {}; bless $self, $class; $self->_init(@_); return $self; } { my @_init_data = qw(name drive body engine_cap); sub _init { my ( $self, $description ) = @_; my %data; @data{@_init_data} = @$description{@_init_data}; %$self = %data; } } sub car_name { my $self = shift; return $self->{name}; }
sub drive_type { my ( $self, $drive ) = @_; if ($drive) { unless ( $drive =~ /^(?:fwd|rwd|4wd)$/ ) { die "$drive not a valid drive type. Use fwd, rwd or 4w +d\n"; } $self->{'drive'} = $drive; } return $self->{'drive'}; } sub body_type { my ( $self, $body ) = @_; if ($body) { unless ( $body =~ /^(?:sedan|wagon|coupe|hatch)$/ ) { die "$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'}; }
} # end of Car class my $car = Car->new( { name => 'Toyota', drive => '4wd', body => 'wagon', engine_cap => + 2010 } ); print join " " => $car->car_name, $car->drive_type, $car->body_type, $car->engine_cap; print $/; $car = Car->new( { name => 'Kia', drive => 'fwd', body => 'sedan', engine_cap => 20 +08 } ); print join " " => $car->car_name, $car->drive_type, $car->body_type, $car->engine_cap;
If I may also advise, please look into A postmodern object system for Perl 5 like Moose or Moo, instead of this intrinsic Perl OOP

If you tell me, I'll forget.
If you show me, I'll remember.
if you involve me, I'll understand.
--- Author unknown to me

In reply to Re: Setting common object attributes by 2teez
in thread Setting common object attributes by nevdka

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.