S
sln
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
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