in reply to Writing Object-Orientated Module: use of “new” and other methods.
My question is how I should arrange the methods in the module?There are many ways. I prefer to have my "constructors" (although what commonly is called a constructor isn't really a constructor - Perl has a constructor, and it's called bless) just create and return an object, and nothing else. Initializing the object is just asking for trouble in the long run; specially if you want to use MI.
So, I'd do something like:
Note also that I call new as a class method, and I'm not using indirect calls. That will bite you sooner or later as well, as the call is ambiguous.package FruitID; sub new {bless {}, shift} sub init { my $self = shift; ... initialize object ... } ... package main; my $testdata = FruitID->new->init("12 bananas");
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Writing Object-Orientated Module: use of “new” and other methods.
by tchrist (Pilgrim) on May 06, 2011 at 02:26 UTC |