Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

Re: Re: Re: Re: Re: Reducing Perl OO boilerplate

by dragonchild (Archbishop)
on Feb 18, 2004 at 19:13 UTC ( [id://330020]=note: print w/replies, xml ) Need Help??


in reply to Re: Re: Re: Re: Reducing Perl OO boilerplate
in thread Reducing Perl OO boilerplate

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:

package Foo; sub new { my $class = shift; my %options = @_; my $self = {}; # Do 1000 lines of stuff with %options return bless $self, $class; } package Bar; use base 'Foo'; # A bunch of stuff goes here, but NO new() method! package main; my $object = Bar->new( # A bunch of named parameters go here );

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:

package My::Base::Class; sub new { my $class = shift; my $self = bless {}, $class; (@_ % 2) && die "Odd number of parameters passed to new()"; my %options = @_; while (my ($k, $v) = each %options) { next unless $self->can($k); $self->$k($v); } return $self; }

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.

------
We are the carpenters and bricklayers of the Information Age.

Please remember that I'm crufty and crochety. All opinions are purely mine and all code is untested, unless otherwise specified.

Replies are listed 'Best First'.
Re: Re: Re: Re: Re: Re: Reducing Perl OO boilerplate
by flyingmoose (Priest) on Feb 18, 2004 at 20:25 UTC
    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:

    No offense, but when I mix concepts, I *mean* to mix concepts :) I don't need the CS 1 / CS 2 intro to OO class. I could teach the class (just not in Perl). Perl's OO syntax isn't pure as it should be (yet), so it takes some effort to figure out the idioms -- which has never been the case in any other language supporting OO that I've picked up.

    But yes, the init hook suggested in the above replies (especially Class::Method_Maker) is what I want. Java and C++ can make use of super constructors, the init hook is exactly what I was asking about, as my named parameter implementation as the OP (sans init hook), had a few glitches that would not provide the things init() provides.

    Essentially my requirements were three-fold: (A) keep the code clean (solved: MethodMaker), (B) No long mamoth constructors, so named params help (solved: hash), (C) need instructors to benefit from inheritance (solved: init hook). Success. However arriving at this was an experience!

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others chanting in the Monastery: (4)
As of 2024-04-25 06:09 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found