Hello:
I'm fairly new to OOP in Perl, though I've been programming in Perl for
about 7 years now, and I am in the process of creating my first classes.
It is a "recursive" class, that is, one of its private members is a
list of other objects of its own type (independent objects themselves,
not clones of their parents.) For example, the constructor looks
something like this:
package MyClass;
sub new
{
my $class = shift;
my $self = { LIST => [] };
bless($self, $class);
$self->_init();
return $self;
}
What I want is to have a method that will automatically add a new object
of the same class to the $self->{'LIST'} member and return a reference
to it. I tried this:
sub add_object
{
my $self = shift;
# perhaps this should be (ref $self)->new() ??
my $new_obj = MyClass->new();
push(@{ $self->{'LIST'} }, $new_obj);
return $new_obj;
}
The above seems to work exactly as I expected. However, being new to
OOP in Perl (but not to OOP or Perl in general!) I wonder if this is
actually the proper way to do it, or if there are some sort of
side-effects that I am not aware of -- or worse -- that the fact that it
works at all is a side-effect in itself!
Thanks for your help!
dZ.
_______________________________________________
Perl-Win32-Users mailing list
[email protected]
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs