more useful options | |
PerlMonks |
Re: Re: Re: Re: Re: Reducing Perl OO boilerplateby dragonchild (Archbishop) |
on Feb 18, 2004 at 19:13 UTC ( [id://330020]=note: print w/replies, xml ) | Need Help?? |
You're mixing concepts. How you pass the parameters to the constructor is completely orthogonal to how you create the object. In Perl, a constructor is just another function. Its first parameter is either the classname or the blessed reference that called the method. The rest of @_ are the parameters passed to the constructor. The only thing a constructor is usually expected to return is a blessed reference to something (or undef, if an error was encountered). IT DOESN'T HAVE TO BE THE SAME CLASS. For example, DBI doesn't return an object blessed into the DBI class.
Mammoth constructors have absolutely nothing to do with inheritance. Inheritance is, for all practical purposes, determined when using the two-part form of bless. Nothing more, nothing less. Here's an example:
Named parameters, inheritance, and a bunch of stuff in a constructor. No sweat. Now, if you wanted to have all your objects use a very basic constructor, let me show you the basics of what I do:
I assume that a mutator has been created with the same name as the parameter being passed in and that you want to assign the value to the attribute corresponding to the mutator. Very simple, very easy ... no fuss - no muss. If everything inherits from My::Base::Class, then everything gets that constructor. Named parameters, inheritance, and no boilerplate constructor. Now, you're going to want more flexibility than that, so you might create an init() hook. You might auto-generate mutators based on a method call at compile time. There's all sorts of sugar you can add to this. But, at the heart, this is all you really need. ------
Please remember that I'm crufty and crochety. All opinions are purely mine and all code is untested, unless otherwise specified.
In Section
Seekers of Perl Wisdom
|
|