Subject: | No way to set a superclass constructor argument from a derived class |
Class-Std 0.0.8
Perl 5.8.8
Linux 2.6.16
In days gone by, I might have done this:
package myclass;
use Carp qw(croak);
sub new {
my ($class, $value) = @_;
croak "required value missing" if @_ == 1 or !$value;
return bless \$value, $class;
}
package mysubclass;
use base qw(myclass);
sub new {
my ($class) = @_;
return bless $class->SUPER::new(23), $class;
}
I see no way to do this with Class::Std. My instinct was to do this:
package myclass;
use Class::Std;
my %value :ATTR( :init_arg<value> );
package mysubclass;
use base qw(myclass);
sub BUILD {
my ($self, $ident, $args) = @_;
$args->{value} = 23;
}
Reading Class/Std.pm, I see why this doesn't work, but I think it
should. At least there should be some mechanism to make this possible.
My "workaround" is to remove the :init_arg from myclass, and setup a
START that croaks if the value isn't there, or sets it if it is. In
other words, I'm doing what I would have done pre-Class::Std, and not
taking advantage of its whole argument checking setup.
Was there a design rationale for not allowing this? Can I get something
like this in the next release? Otherwise, if you think the idea is fine,
I can try to cobble together a patch myself.
Cheers,
Rob.