Beefy Boxes and Bandwidth Generously Provided by pair Networks
Come for the quick hacks, stay for the epiphanies.
 
PerlMonks  

Writing constructors

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

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 12: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 Anonymous Monk on Feb 28, 2005 at 11: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.
Re: Writing constructors
by perlfan (Vicar) on Feb 25, 2005 at 17: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.

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
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others taking refuge in the Monastery: (6)
As of 2024-03-29 11:43 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found