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

How do I make a copy constructor? (inheritance?)

by RiotTown (Scribe)
on Mar 28, 2001 at 22:57 UTC ( [id://68006]=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.

RiotTown has asked for the wisdom of the Perl Monks concerning the following question: (object-oriented programming)

I'm fairly new to this perl-OO stuff, but I am making headway except for a recent problem. I've got a package with the follow constructor and accessor methods, but I'm running into problems with creating a copy constructor.
sub new { my $self; $self->{ A } = undef; $self->{ B } = undef; bless $self, 'Stuff'; } sub A { my ( $self, $a ) = @_; $self->{ A } = $a; }
---- I've gotten the copy() method below to work:
sub copy { my ( $self ) = @_; my ( $new ); %{ $new } = %{ $self }; bless $new, 'Stuff'; return $new; }
The problem that I'm running into is that in the Stuff object I'd like $self->{ B } to be an array of another type of object (same type of structure, much different data). Whenever I call the copy constructor I just get a reference to the same array as the initial object, not a true copy. Any ideas as to what I'm doing wrong, or am I approaching this the wrong way?

Originally posted as a Categorized Question.

Replies are listed 'Best First'.
Re: How do I make a copy constructor? (inheritance?)
by japhy (Canon) on Mar 28, 2001 at 23:27 UTC
    You want a deep copy -- try the Storable module; you want the dclone() function.
Re: How do I make a copy constructor? (inheritance?)
by Anonymous Monk on Jul 09, 2002 at 04:49 UTC
    There is a potential other problem with your code, which I would like to pinpoint as you have talked about inheritance. If your copy method is called for a child of the Stuff class it will generate a Stuff object and not the a Child-Of-Stuff object. In order to avoid this you could bless to ref($self) you might considder it unlikely that a child class will not override the copy constructor, but reasonable applications are thinkable. (e.g.dispatching by class, Stuff is abstract/interface and you just add methods in your child classes etc.) And there is a not OO issue, what if you change the name of your class, you have to change all your bless statements... It is in general a good idea not to hardcode this kind of information.
Re: How do I make a copy constructor? (inheritance?)
by Anonymous Monk on Jun 27, 2003 at 14:13 UTC

    Re: How do I make a copy constructor? (inheritance?)

    Originally posted as a Categorized Answer.

Re: How do I make a copy constructor? (inheritance?)
by BazB (Priest) on Jun 27, 2003 at 17:46 UTC

    merlyn (otherwise known as Randal Schwartz) discusses shallow vs deep copying in one of his columns.

    An alternative to Storable for creating clones/copies of objects is the Clone module.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://68006]
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.