I don't feel at all comfortable with Perl OO, to be honest: it feels... weird to me for some reason.

I think a lot of people give "OO" a lot more fear and awe than it deserves. I think partly this is because of all the multisyllable terms that the jargon freaks just love to insist is the cool parts of OO. (Polymorphism, Inheritance, Method Dispatch, Overloading, etc). Perl's OO is really close to the raw idea of OO: it tightly associates data with the methods/subs that operate on that data. (The fact that Perl also does pretty much the full repretoire of OO, with the exception of Data Hiding, but thats not an essential part of OO in my mind.)

So lets consider this subroutine structure...

sub make_complex_array { my @args=@_; # ... return $complex_array_ref } sub do_funky_stuff_with_complex_array { my $array=shift; # .... return $value; }

To convert this to a class/object all we have to do is add one line and alter two! (Well as a sop to convention and aesthetics we'll rename the subs)

package Complex::Array; sub new { my ($class,@args)=@_; # ... return bless $complex_array_ref,$class; } sub do_funky_stuff { my $array=shift; # .... return $value; }

And whats so hard about this? Now the $array knows that it isnt just an array with indexes, it knows its a complex data strcture with a set of extended behaviour. If we need to cache things about the array we have a nice convient handle that we do so with.

The point of this all is that you want to maintain state through a set of package level variables. This isnt a good idea a general rule (singletons aside). If you need to maintain state across multiple subroutine invocations you should use OO. Especially as in Perl its so damn easy. :-)

my objects really have no attached methods and very little need for privacy.

There is no privacy in Perl so thats no excuse. As for the methods from what youve described all of your subroutines are method candidates to me. If you like post a more beefy code example and ill refactor it into OO for you.

HTH


---
demerphq

<Elian> And I do take a kind of perverse pleasure in having an OO assembly language...

In reply to Re: Re: Re: Design Question by demerphq
in thread Design Question by Anonymous Monk

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.