I
Ivan Shmakov
(ISTR that there was a discussion of this somewhere in the
Perl's documentation, but I cannot find it right now.)
So, I'm implementing a subclass SubClass to the class Class:
package SubClass;
use base 'Class';
Then, how do I write a suitable constructor for SubClass, given
that I need to associate some additional data with the object?
Indeed, I may know that the object is a reference to a hash, so:
sub new {
my ($class, $param_my, @param_super) = @_;
my $self
= Class::new ($class, @param_super)
or die ();
$self->{"SubClass:
aram_my"}
= $param_my;
## .
$self;
}
However, do I understand it correctly that once the Class'
author switches to, say, a reference to a list, I'm out of my
luck?
I wonder, will it help if the Class' author provided a way for
the subclasses to associate arbitrary data with the object?
Consider, e. g.:
package Class;
sub appdata {
my $self = shift;
return ($self->{"appdata"} = shift)
if (@_);
return ($self->{"appdata"});
}
package SubClass;
## a hack: replace appdata with a "nested" version?
sub appdata {
my $self = shift;
my $myself = $self->SUPER::appdata ();
return ($myself->{"appdata"} = shift)
if (@_);
return ($myself->{"appdata"});
}
sub new {
my ($class, $param_my, @param_super) = @_;
my $self
= Class::new ($class, @param_super)
or die ();
$self->SUPER::appdata ({ "param_my" => $param_my });
## .
$self;
}
And will the hack above allow for this to be repeated for
SubSubClass (and then, if done again, further)?
TIA.
Perl's documentation, but I cannot find it right now.)
So, I'm implementing a subclass SubClass to the class Class:
package SubClass;
use base 'Class';
Then, how do I write a suitable constructor for SubClass, given
that I need to associate some additional data with the object?
Indeed, I may know that the object is a reference to a hash, so:
sub new {
my ($class, $param_my, @param_super) = @_;
my $self
= Class::new ($class, @param_super)
or die ();
$self->{"SubClass:
= $param_my;
## .
$self;
}
However, do I understand it correctly that once the Class'
author switches to, say, a reference to a list, I'm out of my
luck?
I wonder, will it help if the Class' author provided a way for
the subclasses to associate arbitrary data with the object?
Consider, e. g.:
package Class;
sub appdata {
my $self = shift;
return ($self->{"appdata"} = shift)
if (@_);
return ($self->{"appdata"});
}
package SubClass;
## a hack: replace appdata with a "nested" version?
sub appdata {
my $self = shift;
my $myself = $self->SUPER::appdata ();
return ($myself->{"appdata"} = shift)
if (@_);
return ($myself->{"appdata"});
}
sub new {
my ($class, $param_my, @param_super) = @_;
my $self
= Class::new ($class, @param_super)
or die ();
$self->SUPER::appdata ({ "param_my" => $param_my });
## .
$self;
}
And will the hack above allow for this to be repeated for
SubSubClass (and then, if done again, further)?
TIA.