in reply to Re: Subroutines with differing behaviour created from AUTOLOAD
in thread Subroutines with differing behaviour created from AUTOLOAD
However, the method will have issues for other reasons. The code implies that two difference instances might have the same field be editable or not at the instance level. If that's the case, then the first instance to access the field will set its editable-ness for all instances of the class.
There are two solutions. If each instance needs to maintain editability, then you have to go to the OP's solution, which is to have AUTOLOAD evaluate everything on a case by case basis. (This can still be gotten around, but you'd have to attach the closures to the instance and have the class methods dispatch to the instance's method. Too much work!)
If the fields are editable based on the class, then hoist the editability to the class level and deal with it there.
my $class = ref($self); my %editable = do { no strict 'refs'; %{"${class}::__EDITABLE__"}; }; die "No such method $AUTOLOAD. Barfing\n" unless exists $editable{$field}; if ($editable{$field}) { *$AUTOLOAD = sub { my $self = shift; $self->{$field}{value} = shift if @_; $self->{$field}{value}; }; } else { *$AUTOLOAD = sub { my $self = shift; die "Can't modify readonly field '$field'" if @_; $self->{$field}{value}; }; } goto &$AUTOLOAD;
------
We are the carpenters and bricklayers of the Information Age.
Then there are Damian modules.... *sigh* ... that's not about being less-lazy -- that's about being on some really good drugs -- you know, there is no spoon. - flyingmoose
I shouldn't have to say this, but any code, unless otherwise stated, is untested
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^3: Subroutines with differing behaviour created from AUTOLOAD
by hv (Prior) on Jun 08, 2004 at 16:15 UTC | |
by davis (Vicar) on Jun 08, 2004 at 16:20 UTC | |
by dragonchild (Archbishop) on Jun 08, 2004 at 16:56 UTC | |
|
Re^3: Subroutines with differing behaviour created from AUTOLOAD
by davis (Vicar) on Jun 08, 2004 at 16:16 UTC | |
by dragonchild (Archbishop) on Jun 08, 2004 at 16:55 UTC |