Beefy Boxes and Bandwidth Generously Provided by pair Networks DiBona
Do you know where your variables are?
 
PerlMonks  

Writing constructors

by crenz (Priest)
on Feb 25, 2005 at 04:04 UTC ( [id://434407]=perlquestion: print w/replies, xml ) Need Help??

This is an archived low-energy page for bots and other anonmyous visitors. Please sign up if you are a human and want to interact.

crenz has asked for the wisdom of the Perl Monks concerning the following question:

For years now, I've been using something like this as my default constructor:

sub new { my $class = shift; my %params = @_; my $self = {}; foreach (qw(param1 param2 param3)) { $self->{$_} = $params{$_} } # further initialization. # ... bless $self, $class; }

Now I'm transitioning to using fields, as I quite like the idea and it's been in core since 5.6.1. The documentation for fields suggests something like this:

use fields qw(param1 param2 param3); sub new { my ClassName $self = shift; unless (ref $self) { $self = fields::new($self); } # initialization # ... return $self; }

So, my questions are: Is bless $self, $class being phased out in favour of the my HardCodedClassName $self form? Are there any benefits to this? Will inheritance still work even despite the hard coded class name?

Update: Sorry, I saw that for subclasses, fields still recommends the my $class = shift form. So I don't need to bless $self, class anymore, since fields does that for me, but I don't have to specify the class name in the code.

Replies are listed 'Best First'.
Re: Writing constructors
by brian_d_foy (Abbot) on Feb 25, 2005 at 07:06 UTC

    If you look under the hood, you'll see that fields still uses the two-argument form of bless. It's just hiding it from you. It happens when you call fields::new().

    The examples you see in the docs really just passes the first argument to fields::new, although it gives the first argument different names, and then passes that to fields::new() that uses it as the second argument to bless().

    It's always seemed to mind-bending for me, so I don't use it. I can do without the black magic. :)

    --
    brian d foy <bdfoy@cpan.org>
Re: Writing constructors
by perlfan (Parson) on Feb 25, 2005 at 12:02 UTC
    I tend to use:
    sub new { my $class = shift; bless { _att1 => $_[0], ... _attN => undef, }, $class; }
    I like to initialize a member attribute field in the blessed hash, even if it is not neccessarily defined during construction. I also like to minimize the number of input parameters in my constructors, but that really is just a personal preference.
Re: Writing constructors
by Anonymous Monk on Feb 28, 2005 at 06:34 UTC
    I prefer to not populate my hash during the construction phase, but use a separate method for that. The reason is multiple inheritance. If you do multiple inheritance, it will be that at least one of the constructors isn't called, or that the object from at least one of the constructors (assuming you are using 2 classes - it's worse with more) won't survive. If both constructors return populated objects, the inheriting class needs to break encapsulation, and construct a resulting object by mucking with the internals. It becomes a lot easier if you have a separate method to (initially) populate your objects.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://434407]
Approved by holli
help
Sections?
Information?
Find Nodes?
Leftovers?
    Notices?
    hippoepoptai's answer Re: how do I set a cookie and redirect was blessed by hippo!
    erzuuliAnonymous Monks are no longer allowed to use Super Search, due to an excessive use of this resource by robots.