http://qs321.pair.com?node_id=498002


in reply to Object Constructors - Advice of experienced OO Perl Monks

To answer your initial question -- that depends on what you are trying to solve. :) You need to be more specific to get a answer with more value than that. You need to think in terms of readability, maintainabilty, correctness, robustness, etc.

Here is a constructor i like use when the situation is quick and dirty

sub new { bless {}, shift }
That's all you need -- you can add attributes later. However, if i am writing production code, i might opt for the more verbose version:
sub new { my $class = shift; my $self = { foo => 'bar', baz => 'qux', }; return bless $self, $class; }

I try to use Class::MethodMaker whenever i can, i just like that module. Here is an example where i use that module to turn XML into real objects: XML::Simple + Class::MethodMaker. Hope this helps. :)

jeffa

L-LL-L--L-LL-L--L-LL-L--
-R--R-RR-R--R-RR-R--R-RR
B--B--B--B--B--B--B--B--
H---H---H---H---H---H---
(the triplet paradiddle with high-hat)