in reply to Inheritence of a Constructor that uses a file-scoped lexical?
This is an example of the Strategy pattern.
package Some::Package; sub new { my $proto = shift; my $class = ref($proto) || $proto; my %args = @_; my $fields_hr = $class->fields; my $self = bless { _permitted => $fields_hr, %$fields_hr, }, $class; foreach my $arg ( keys %args ) { $self->$arg( $args{$arg} ); } $self } sub AUTOLOAD { my $self = shift; my $type = ref($self) or croak "$self is not an object"; my $name = $AUTOLOAD; $name =~ s/.*://; exists $self->{_permitted}->{$name} or croak "Can't access `$name' field in class $type"; @_ and $self->{$name} = shift; $self->{$name} } # the base class also defines some field(s). sub fields { return { name => undef } } # # A derived class need only define the fields, and any other # class-specific data/behavior. # package Some::Package::Sub; use base qw(Some::Package); sub fields { return { name => undef, needthis => undef, } }
|
|---|