nevdka:

To expand a little on rjts post: If all your car objects basically act the same but have different attributes then you don't really need to subclass. You just need a simple way to apply default attributes when you build your cars. You could have a set of standard attributes based on the car type, something like:

# You could load the hash from a database table, or just call the data +base for # each car. I'm just hardcoding for this example: my %car_models = ( Corolla => { drive=>'fwd', body=>'hatch', engine_capacity=>1798 }, Tercel => { drive=>'fwd', ... } ); while (<DATA>) { chomp; my $car = Car->new(); my $model_attributes = $car_models{$_}; $car->drive_type($model_attributes->{drive}); $car->body_type($model_attributes->{body}); $car->engine_cap($model_attributes->{engine_capacity}); ... }

This way, you just look up the attributes based on the model and add them when you build your car. You could even have a 'load_defaults_for_model' function in your car class to automatically do the lookup when you create your car:

sub load_defaults_for_model { my $self=shift; my $model_attributes = $car_models{$self->{Model}}; $self->drive_type($model_attributes->{drive}); $self->body_type($model_attributes->{body}); $self->engine_cap($model_attributes->{engine_capacity}); } sub new { my ($class, $model) = @_; my $new_obj = { .... build your object .... }; bless $new_obj, $class; if (defined $model) { $new_obj->{model} = $model; $new_obj->load_defaults_for_model(); } return $new_obj; }

...roboticus

When your only tool is a hammer, all problems look like your thumb.


In reply to Re: Setting common object attributes by roboticus
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.