The problem does not seem to be that Perl is better, it just seems like your system is in a serious need for refactoring. Here we go!

Step 1 - get a parent class for A and B. If they have the same interface in the code, inforce it and make your life easier. Based one the code, then, SomeFlag and SomeValue become irrelevant, so....

for (int i=0; i < SomeValue; i++) { Class_P *p; //parent class int SomeID; p = (Class_p)* some_param; SomeID = p->getID;

Step 2 - Eliminating the switch...case. Many times, a case statement indicates some problems with the abstraction. So, lets create a new class called SystemCase for our purposes. It will be abstract with a class method called getInstance. Each individual case will be its own object. Since I can't beleive that these cases are only relevant in this chunk of code, this should help elsewhere. Since all of this is decided by the getID() method of p, there are some additional refactorings gained later...

class SystemCase{ public: SystemCase getInstance(Class_P *p){ switch(p->getID()){ case FIRST_CASE: return FirstCase(p); ... } virtual SomeArrayType runMethod(); }; class FirstCase{ public: SomeArrayType runMethod(){ ... } };

So, that takes the whole chunk of code to...

for (int i=0; i < SomeValue; i++) { Class_P *p; //parent class int SomeID; p = (Class_p)* some_param; //SomeID = p->getID() isn't needed since we look at this in System +Case SystemCase sc = SystemCase::getInstance(p); SomeArray[i].value = sc.runMethod(); }

Eleven lines here, and the code is much more clear than before. You did a great job refactoring the code in Perl, but the same thing can be just as easily done in C++. As I mentioned above, if getID() is determining values to run elsewhere, the above refactoring should help out throughout your system.


In reply to Re: Be grateful for Perl OO by Steve_p
in thread Be grateful for Perl OO by dragonchild

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.