Greeting Monks. This is my first post in this wonderful forum

Help me code this scenario better.I have a baseclass named "automobile", and two derived classes named "car" and "truck". Also I have an new class named "ParkingLot", which should know what the car it is operating on, to customize the lot-size and other attributes.

Now back to the problem. The current codebase that I work on is matured and I wouldn't be able to make drastic changes. It's complete written in Perl OOPS. The "ParkingLot" constructor will always be passed with the automobile object (instantiated much before this) as an argument. The class(Parking Lot) uses this argument, does a ref on object pointer to find whether the class is "car/truck" and does some very specific operations based on vehicle type. To avoid this, I tried to make interfaces more generic and try to abstract the code inside refs to more meaningful public attribute or a private function, which can be overriden by derived class (Ex: parkingLotForTrucks and parkingLotForCars). IMHO, this would make the code more maintainable.

Where I cannot modularize/carve out functions out of code due to lack of context, for example code like below are found in lot of places (mostly the RHS of assignment is a constant number). I want to know, if I could use constants and override them in derived class, instead of defining single line functions that are not reusable?

For example, the below snippet could be changed as
if (ref $automobile eq "car") { set x = y; } else (ref $automobile eq "truck") { set x = z; } #changed to use constant Y => "STRING1"; # In Base class use constant Y => "STRING2"; # In Derived Class #In Baseclass I'll have, set x = Y;
I have read constant in baseclass could be overridden in derived class. But somehow I feel this will make code less readable, than using if-else. But it's less scalable than the Const method. I would like to know how Monks would go about this problem and make the code elegant and maintainable?

In reply to using constants in Derived class by rajachan

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.