I am having a first go at writing an Object-Orientated Module and was hoping for some general advice as to the proper use of methods.

The module is a simple text-parsing one which aims to help index some technical documentation. The idea is that if I fed in a phrase like "12 bananas" it could do something like:

$testdata = new FruitID("12 bananas"); print $testdata->quantity; #"12" print $testdata->fruit; #"banana" print $testdata->colour; #"yellow"
The code is therefore quite simple--it mostly consists of if ($stringtotest =~ ...) clauses--but quite long since it’s value is in the technical vocabulary.

My question is how I should arrange the methods in the module?

Should I arrange the code so all of the if clauses (i.e. the heavy lifting of the module) are in the "new" method? Or should I use "new" method simply for declaring and blessing the constructors and add an intermediate method (e.g. $testdata = new FruitID("12 bananas"); $testdata->process; print $testdata->quantity; #"12")? Or should I try to break up the code so that the processing is only done when one uses a method which expects to return a value (e.g. methods like $testdata->quantity;).

I presume that if all the values are likely to be requested at the same time and if the tests are very interlinked (e.g. I need to know fruit="banana" before I can return colour="yellow") then it is best to add the heavy code to the "new" method or a process method. However, I am not sure which of these I should choose and the factors, if any, I should consider.

Thanks!


In reply to Writing Object-Orientated Module: use of “new” and other methods. 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.