Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical

Writing constructors

by crenz (Priest)
on Feb 25, 2005 at 09:04 UTC ( #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 <>
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?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://434407]
Approved by holli
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others avoiding work at the Monastery: (2)
As of 2022-01-27 20:29 GMT
Find Nodes?
    Voting Booth?
    In 2022, my preferred method to securely store passwords is:

    Results (71 votes). Check out past polls.