in reply to Re: Inheritence of a Constructor that uses a file-scoped lexical?
in thread Inheritence of a Constructor that uses a file-scoped lexical?
If you don't like bodging in the symbol table (won't work under strict), it might be smoother would be to have a fields accessor sub in each class that would simply return the %fields hash:
This uses true polymorphism by calling $class->fields. You can keep the %fields hash as a lexical within the classes as well.package SomePackage; my %fields = ( name => undef ); ## can be a lexical ### here's a new simple accessor sub fields { return %fields }; sub new { my $proto = shift; my $class = ref($proto) || $proto; my %args = @_; ### added this line my %fields = $class->fields; my $self = { _permitted => \%fields, %fields, }; bless($self, $class); foreach my $arg (keys(%args)) { $self->$arg($args{$arg}); } } ## ... elsewhere package Some::Package::Sub; use base 'Some::Package'; ### you needed this line before my %fields = ( name => undef, required => undef ); sub fields { return %fields };
blokhead
|
|---|