I am going to comment on this from a different direction, namely *why* object-oriented programming. Understanding this makes a lot of things easier to understand.

There are basically two kinds of bugs in software: those that involve doing the wrong thing with the right data, and those that involve doing the right thing with the wrong data. Of the two, the second is far harder to track down and fix because the data bacame wrong somewhere else and that has to be tracked down. For this reason state management is a central problem in programming.

Looking at your code you seem close to structural programming. The idea of structural programming is you keep data together that belongs together and this makes it easier to determine when data is bad. With your hashes, this is what you are doing. It also simplifies assumptions your code has to make.

The next step is to go object oriented and tie the data to behavior. Ideally state should only be changed by going through the behavior. This gives you fewer points to track down when looking for how data became bad. In Perl you use packages and bless to do this, but you can also use Moose which is a really nice framework.

One reason to recommend Moose btw is that it makes the next step (after going OO) a lot easier, namely making your objects immutable. With immutable objects you have only one place to check data validity (the constructor) and so it becomes nearly possible to eliminate this whole class of bugs.

This latter point gets you closer to functional programming because if your output is dependent only on your input (i.e. instead of changing an object you return a new one), you can manage state well enough to have programs which safely write programs.


In reply to Re: How do I go from procedural to object oriented programming? by einhverfr
in thread How do I go from procedural to object oriented programming? by Lady_Aleena

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.