in reply to Re^4: "Fields" for "Objects"
in thread "Fields" for "Objects"
... or doing some weird and nasty local/typeglob hackery.
Keep in mind that talking about Perl5, the specification is the implementation. So that "weird and nasty hackery" is, by flipping the enlightenment bit, Higher Order Perl... ;)
I'd say - dealing with Perl5, expect dragons. But they are teaching dragons. We even do have one here...
update:
Of course, this too is solveable, Ovid had a hack where he assigned a name to the __ANON__ typeglob slot using local I don't recall the details, and there is also Sub::Name on the CPAN. But either way your now depending on an XS module (Sub::Name) or doing some weird and nasty local/typeglob hackery.
Nah... look, ma, no XS, no __ANON__:
sub AUTOLOAD { my $self = shift; my $type = ref($self) or croak "$self is not an object"; my $name = $AUTOLOAD; $name =~ s/.*://; # strip fully-qualified portion unless (exists $self->{_permitted}->{$name} ) { croak "Can't access `$name' field in class $type"; } eval ("sub $AUTOLOAD { my (\$self,\$value) = \@_; \$self->{$name} = \$value if defined \$value; \$self->{$name}; }"); die $@ if $@; goto &$AUTOLOAD; }
|
---|