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

Hi,

First of all, please avoid the word fields as it is very confusing in OO. (pseudo hashes, etc...) as they are not lexically scoped (don't use my with fields).

Second, I see no inheritance operator in your classSome::Package::Sub. It misses the line

use base qw(Some::Package);
Third: The line my %fields is totally useless in the package Some::Package::Sub, as it is lexically scoped, and only consultable from with the package itself. I would suggest to create an init subroutine, and call this from inside the new statement. f.e.
sub new { my $proto = shift; my $class = ref($proto) || $proto; my %args = @_; bless($self, $class); $self->init(); foreach my $arg (keys(%args)) { $self->$arg($args{$arg}); } }
And in the Some::Package::Sub
sub init { my $self=shift; $self->{_permitted} = \%fields; #... and add the other fields also };
It least, this is the way I would do it, but since I am not perfect....
---------------------------
Dr. Mark Ceulemans
Senior Consultant
IT Masters, Belgium

Replies are listed 'Best First'.
Re: Re: Inheritence of a Constructor that uses a file-scoped lexical?
by Orsmo (Beadle) on Oct 29, 2002 at 14:33 UTC

    Well, that's all well and good, since it does solve the problem of making sure that the reference to %fields points to the one in the appropriate file, but it sort of recreates the original problem I was trying to solve.

    The whole point of inheriting the constructor was that I had subclasses that were all using esentially the same code for a constructor, so why duplucate the code. If I made changes, I'd have to update each package, etc. Instead, inherit it. But that introduced the problem that started this thread.

    Your solution creates an init method, which must now exist in the same file as the subclass, and thus much exist in the same file for each and every subclass. While it significantly reduces the amount of code that is reproduced in each module, it doesn't eliminate it entirely.

    If I make init inheritable from the base clase though, then I run into the same problem again. The reference points to the hash in the wrong file. It seems to be a chicken and the egg problem. Perhaps file-scoped lexical isn't the right way to go, but then how else do I get the fields (a name I shall change now, thank you for the education) for each class set up if not through a file-scoped lexical?