Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > Cloning classes, deep copy revisited (Was: Copy Consturctor in Perl) ..

Reply
Thread Tools

Cloning classes, deep copy revisited (Was: Copy Consturctor in Perl) ..

 
 
sln@netherlands.com
Guest
Posts: n/a
 
      11-23-2008
I've got a few packages declared in on file. I need a clone/copy
type functionality for all the ones dealing with a high probability
they will be duplicated (but maybe not) in mass. So I need a clone/copy
type functionality. Some classes need 'deep' cloning, some don't.

I came up with this. See any potential problems using this scenario?
I have given all the classes this same mechanism.

Added comments for usenet reading.
Thanks!


sln
-----------------------------------------------------------------
Example:

package ReS;

sub new
{
my $self;
my $class = shift;
if (defined($_[0]) && ref($_[0]) eq 'ReS') {
$self = {};
my $obj = bless ($self, $class);
return copy($obj, $_[0]);
}
$self = {
...
};
# Should we risk calling set_val that does the
# same thing at the expence of a function call ?
# Sure we could do &set_val.. but still overhead
while (my ($name, $val) = splice (@_, 0, 2)) {
next if (!defined $val);
if (exists $self->{lc $name}) {
$self->{lc $name} = $val;
}
}
return bless ($self, $class);
}
sub clone
{
# clone self, return new
return ReS->new( $_[0]);
}
sub copy
{
# copy other to self, return self
return $_[0] unless (defined $_[1] && ref($_[1]) eq 'ReS');
%{$_[0]} = %{$_[1]};
# do specific deep copying..
# see article on dclone function
$_[0]->{'something'} = ();
@{$_[0]->{'something'}} = @{$_[1]->{'something'}};
return $_[0];
}

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Cloning revisited Alf P. Steinbach C++ 1 08-31-2009 07:36 PM
Experimental only: Pointer copy consturctor does not work nagrik C++ 5 10-25-2006 10:36 AM
what is Deep Copy, shallow copy and bitwises copy.? saxenavaibhav17@gmail.com C++ 26 09-01-2006 09:37 PM
gcc needs copy consturctor for inplace constructed object passed to func as reference, others not nutty C++ 6 06-15-2006 02:02 PM
is dict.copy() a deep copy or a shallow copy Alex Python 2 09-05-2005 07:01 AM



Advertisments