I do actually use OO quite a lot when I'm building modules. I'll be first to admit that I'm perhaps guilty of looking to the OO option first as I prefer the style, but I also (I believe) know those times when a vanilla module would fit the bill just as well.

As to when to use OO it normally follows that I do it in the situations where I can visualise the problem as an object.

When I built my email module it seemed natural to me that it should be an object - an email is a "thing" (read: object) that has properties associated with it ("to" address, body text et al) and methods that apply ("create email","add attachment","send" etc).

The email is the central object around which it hinges.

On the flip side, consider the example of the Math::Trig module. Where would the object be? The input value? - but there may be more than one. The result? - but until it runs there isn't one. Instead it creates a set of functions that can be used more generally.

To your second question - I wouldn't say that OO is "prefered". It's all very much dependant on the application concerned - it's quite possible that in one context using OO is the way to go, whereas in another on the same problem it may be viewed as overkill.

This is just my view, but I hope this helps.

--- Jay

All code is untested unless otherwise stated.


In reply to Re: To use or not to use OO Perl by gothic_mallard
in thread To use or not to use OO Perl by monkfan

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.