in reply to Inheritence of a Constructor that uses a file-scoped lexical?

Just a shot in the dark (I'm an OO newbie as well), but could you not specify your %fields reference with the class in question? In your Some::Package::new method:
my $self = { _permitted => \%{$class::fields}, %{$class::fields}, };

If you then call Some::Package::Sub->new, it uses Some::Package::Sub::%fields, which I think is what you want.

Caveat: I have not tested the above code so, although the syntax checks OK, it might not work at all. But even then, it should give you an idea on how to proceed.

CU
Robartes-

Replies are listed 'Best First'.
Re: Re: Inheritence of a Constructor that uses a file-scoped lexical?
by blokhead (Monsignor) on Oct 29, 2002 at 18:24 UTC
    %fields was defined as a lexical my variable, so you cannot access it from the symbol table (ie, %{"${class}::fields"}). Define the hash as an our global variable (or use vars qw/%fields/; globals) in both classes and you can do it. This uses a taste of polymorphism (not really true polymorphism) in the new constructor -- no matter what inherited class it was called from, it uses that class's %fields hash.

    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:

    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 };
    This uses true polymorphism by calling $class->fields. You can keep the %fields hash as a lexical within the classes as well.

    blokhead