O wise and benevolent monks,

I have just started a new job as a Perl programmer, and have exhausted the training materials my new masters have given me, so I am seeking wisdom on my own. I come from a physics background, and the code I wrote there was far from object oriented. 'Functional' would have been generous, and maybe 'chaotic' most accurate. However, my new masters are insisting on those pesky ideas of readability, maintainability, and reliability, and use OO everywhere. I'm sure I'll be thankful once I start writing production code.

I have been creating an object model for a parking lot. My first attempt had 'Corolla' as a subclass of 'Toyota', as a Corolla was a more specific type of Toyota. However, I now realise that only the attributes are different; all the methods are the same. So, a 'Car' class would be enough:

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, coup +e 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;

However, every Corolla will share many of its attributes with every other Corolla. Every time I create a new Corolla, I would need to set:

my $car = Car->new(); $car->drive_type('fwd'); $car->body_type('hatch'); $car->engine_cap('1798');

And whatever other attributes I put in. I would like to be able to do something like this:

my $car = Corolla->new;

But that means having a subclass, which is what I want to avoid. Is there a better way to set common attributes?


In reply to 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.