Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:
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:
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.$testdata = new FruitID("12 bananas"); print $testdata->quantity; #"12" print $testdata->fruit; #"banana" print $testdata->colour; #"yellow"
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!
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Writing Object-Orientated Module: use of “new” and other methods.
by chromatic (Archbishop) on May 06, 2011 at 01:13 UTC | |
|
Re: Writing Object-Orientated Module: use of “new” and other methods.
by CountZero (Bishop) on May 06, 2011 at 06:23 UTC | |
|
Re: Writing Object-Orientated Module: use of “new” and other methods.
by JavaFan (Canon) on May 06, 2011 at 00:36 UTC | |
by tchrist (Pilgrim) on May 06, 2011 at 02:26 UTC |