Slash Boxes
NOTE: use Perl; is on undef hiatus. You can read content, but you can't post it. More info will be forthcoming forthcomingly.

All the Perl that's Practical to Extract and Report

use Perl Log In

Log In

[ Create a new account ]

pdcawley (485)

  (email not shown publicly)
AOL IM: pdcawley (Add Buddy, Send Message)

Journal of pdcawley (485)

Friday March 15, 2002
04:38 AM

Thinking about constructors again

[ #3552 ]

I've been thinking a bit more about constructors and the fact that being able to do Class->new(file => 'blah') is a really handy thing to do. And then I thought about Perl's diverse object representation model. So, here's the base class 'new' I'm thinking of moving to. Note that it's representation independent and, because I did the 'composed method' thing, it's easy to override small parts of the implementation.

    sub new {
        my $proto = shift;
        return $proto->new_with_args(@_) if @_;

    sub make_a_new_one {
        my $proto = shift;
        bless {}, ref($proto) || $proto;

    sub new_with_args {
        $self->throw(Exception->new("Can't handle args"))

    sub init { }

Note that, if you have an object that's better represented as an arrayref, say, just do

    sub make_a_new_one {
        my $proto = shift;
        bless [], ref($proto) || $proto;

And it will Just Work.

And yes, I *know* it looks like an awful lot of work just to make an object, but it's all about the intent baby.

The Fine Print: The following comments are owned by whoever posted them. We are not responsible for them in any way.
More | Login | Reply
Loading... please wait.