Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

Re: Object Constructors - Advice of experienced OO Perl Monks

by jeffa (Bishop)
on Oct 06, 2005 at 17:53 UTC ( #498002=note: print w/replies, xml ) Need Help??


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)

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://498002]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others perusing the Monastery: (3)
As of 2022-08-17 07:14 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?