develooper Front page | perl.perl5.porters | Postings from January 2000

Re: On Pseudohashes

Tom Christiansen
January 17, 2000 05:32
Re: On Pseudohashes
Message ID:
>Tom Christiansen <> writes:
>>    * Compile-time detection of the validity of $obj->attribute()
>>      in addition to just $obj->{"attribute"}.  In the general case,
>>      this lies somewhere between hard and impossible.  

>How so? In both cases you have to "know" what class $obj is/can be.

>my Dog $spot;

>Gives as much or as little information to either. 

>   $spot->{'tail_colour'}
>   $spot->bark;

>You either look in $Dog::FIELDS{'name'} or CV of $Dog::{'name'} 
>(Perhaps %FIELDs should have been part of the stash ...)

I'm not sure, but I think I was just worried about figuring out
whether a particular assignment was legal:

    my Dog $spot = frobnicate();

But that doesn't matter.  All you care about is whether the methods
invoked against whatever happens at run time to have taken up
residence in the $spot variable.  So long as that class's @ISA and
package symbol table are populated at compile-time with the same
data as they'll ever hold at run-time, you could do the check.
Likewise recursively for parent classes.

The presence of an AUTOLOAD anywhere in the graph seems to make
compile-time validation pointless.  One might have to restrict
people to using "real" method calls, such as those generated via
closures in a loop, to generate compiler-detectable cases:

    for $attr (@attrlist) {
	*$attr = sub { .... };

rather than 

    sub AUTOLOAD {
	my $self = shift;
	my $attr = $AUTOLOAD;
	$attr =~ s/.*:://;
	die "bad call $AUTOLOAD" unless $ok_attr{$attr};

--tom Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at | Group listing | About