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.


In reply to Re: Re: Re: Re: Re: Reducing Perl OO boilerplate by dragonchild
in thread Reducing Perl OO boilerplate by flyingmoose

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.