fce2 has asked for the wisdom of the Perl Monks concerning the following question:
Now in Class::Std, superclass "constructors" (ie BUILD and START) are called automatically. However, there doesn't seem to be a way to set or override the arguments for a superclass in a subclass. I'd like to do something like 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; }
This doesn't work. Even though Class::Std calls BUILD(), then initialises the object from the constructor arguments, and then calls START(), the third argument passed to each BUILD/START method is specific to that method - its discarded afterwards. There doesn't seem to be any way to influence the arguments in this way. Doing 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; }
gives:mysubclass->new;
Anyone got any ideas before I open a ticket in RT?Missing initializer label for myclass: 'value'. Fatal error in constructor call
Cheers,
Rob.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Overriding arguments in Class::Std superclass constructors
by lima1 (Curate) on Aug 11, 2006 at 14:35 UTC | |
by fce2 (Sexton) on Aug 13, 2006 at 04:06 UTC | |
|
Re: Overriding arguments in Class::Std superclass constructors
by jdhedden (Deacon) on Aug 11, 2006 at 15:59 UTC | |
by fce2 (Sexton) on Aug 13, 2006 at 03:46 UTC | |
|
Re: Overriding arguments in Class::Std superclass constructors
by fce2 (Sexton) on Aug 29, 2006 at 08:08 UTC |