What's the benefit of calling new on an object instance?

Posted by Geo on Stack Overflow See other posts from Stack Overflow or by Geo
Published on 2009-10-25T16:48:40Z Indexed on 2010/04/06 1:03 UTC
Read the original article Hit count: 201

Filed under:
|
|
|

I'm reading [Programming Perl][1], and I found this code snippet:

sub new {
    my $invocant = shift;
    my $class   = ref($invocant) || $invocant;
    my $self = {
        color  => "bay",
        legs   => 4,
        owner  => undef,
        @_,                 # Override previous attributes
    };
    return bless $self, $class;
}

With constructors like this one, what's the benefit of calling new on an object instance? I assume that it's what it's for, right? My guess is that if anyone would want to write such a constructor, he would have to add some more code that copies the attributes of the first object to the one about to be created.

© Stack Overflow or respective owner

Related posts about perl

Related posts about oop