Well, I think I may have finally fixed it. The following appears to work. Thanks again to all who helped me understand this problem better.

James

#!/usr/bin/perl

package Class::Initializable;
our $VERSION = 1.0.0;

use 5.6.0;
use strict;
use warnings;

sub new {
my $class = shift;
$class = ref $class if ref $class;
my %args = @_;
my $self = bless { }, $class;
my %seen = (__PACKAGE__, 1);
{
no strict 'refs';
foreach (grep !$seen{$_}++, @{ "${ class }::ISA" }) {
&{ "${ _ }::initialize" }($self, %args);
}
}
$self->initialize(%args);
return $self;
}

sub initialize { die "Abstract method: must overriden!\n"; }

1;
__END__

On Thursday, October 17, 2002, at 08:42 PM, James Edward Gray II wrote:

Thanks all for the constructor suggestions, but I'm still having a few problems. See comments below:

On Thursday, October 17, 2002, at 09:58 AM, NYIMI Jose (BMB) and Jeff 'japhy' Pinyan wrote:

On Oct 17, James Edward Gray II said:

sub new {
	my $class = shift;
	$class = ref $class if ref $class;
	my %args = @_;
	my $self = bless { }, $class;

	# needs hierarchical initialization here

	$self->initialize(%args);
	return $self;
}
	$self->SUPER::initialize(%args);
This doesn't support multiple inheritance, which will be needed to use this class as the "mixin" it is intended to be.

{
no strict 'refs'; # sorry, but it's a bit more convenient this way
for (@{ "${class}::ISA" }) {
$self->initialize(%args);
}
}
This calls the object's initialize method repeatedly, not the initialize of the parent objects.

Using these, I got close, I think. I now have:

my %seen = (__PACKAGE__, 1); # to keep this class from being called
{
no strict 'refs';
$_->initialize(%args) foreach (grep !$seen{$_}++, @{ "${class}::ISA" });
}
$self->initialize(%args); # the actual object

Unfortunately this doesn't quite get it done either. It does call the initialize method of the parents, but the don't get passed an object to modify. I just can't seem to get my head around this one, so any more tips are definitely welcome. Thanks again.

James


--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to