Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > Copy constructor and Dup method in Perl

Reply
Thread Tools

Copy constructor and Dup method in Perl

 
 
sln@netherlands.com
Guest
Posts: n/a
 
      11-19-2008
Hi, I have a need for a Class copy constructor and wonder
if anybody has implemented this.

I wan't to turn a unwieldy hash into a class. It is primarily
a data object with a few methods for retreval of formatted data.
It is going to be passed around as a parameter to several methods
of a different class where data will be fleshed out. Instead of
endless conditional testing of the hash, I thought I would just
validate it as a reference to a data class object.

But I would need a copy constructor and/or a duplicate method.
This is because the object is reused and parameters will change
and because the object might need to be saved and stored at any
particular time.

Example:

package XReS;

sub new
{
my ($class, @args) = @_;
my $self = {
.. set default params ..
};
if (defined($args[0]) && ref($args[0]) eq 'XReS') {
%{$self} = %{$args[0]};
}
else {
while (my ($name, $val) = splice (@args, 0, 2)) {
.. process other args ..
}
}
return bless ($self, $class);
}
sub Dup
{
return XReS->new(shift);
}

Also, do you forsee much more memory overhead by using a
class object instead of just a hash, possibly creating thousands?

Any other pitfalls?
Thanks.


sln

 
Reply With Quote
 
 
 
 
Jim Gibson
Guest
Posts: n/a
 
      11-20-2008
In article <>,
<> wrote:

> Hi, I have a need for a Class copy constructor and wonder
> if anybody has implemented this.


See the dclone function of the Storable module or the article
<http://www.stonehenge.com/merlyn/UnixReview/col30.html>

--
Jim Gibson
 
Reply With Quote
 
 
 
 
sln@netherlands.com
Guest
Posts: n/a
 
      11-21-2008
On Wed, 19 Nov 2008 17:36:55 -0800, Jim Gibson <> wrote:

>In article <>,
><> wrote:
>
>> Hi, I have a need for a Class copy constructor and wonder
>> if anybody has implemented this.

>
>See the dclone function of the Storable module or the article
><http://www.stonehenge.com/merlyn/UnixReview/col30.html>


I knew about deep copy. I read the whole article. I didn't look
at the dclone function yet (as they recommend on that page).

I found it interresting, the recursive sub to find array's that
need to be dup'd. And I'm even better appretiative of Dumper now.

I had some anonymous sub ref's hanging around, wondered about them.

One thing is that if you 'empty' an array, reference or not, by
declaring it with () again, does it create another reference to it
(anonymous or if a reference is taken of it before its cleared)?

I haven't looked at it specifically yet in terms of deep copy but
something like this I will have to resolve:

$href = {};
$href->{aray} = ['1','2'];
or
@tt = ('1','2');
$href->{aray} = \@tt;

@{$href->{aray}} = ();

I know $href->{aray} is a reference,
but if a copy of $href was made,
before it was cleared with = (), is the data
still intact in the copy, or would I have to
do an explicit $newhref->{aray} = [@{$href->{aray}}] ?

Convuluted. Thanks!

sln
 
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
template copy constructor vs normal copy constructor cinsk C++ 35 10-10-2010 11:14 PM
Rubyzip - `dup': can't dup NilClass (TypeError) Luka Stolyarov Ruby 10 09-11-2010 12:13 PM
A constructor calling another constructor (default constructor)? Generic Usenet Account C++ 10 11-28-2007 04:12 AM
:s.respond_to?(:dup) && :s.dup raises François Beausoleil Ruby 1 04-05-2007 05:55 PM
Copy constructor hides default constructor Aire C++ 3 01-25-2004 05:47 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57