Some time ago (nearly 15 years, actually) in this thread, btrott was talking about various ways of protecting a blessed object and quite a lot of discussion came from it.
I haven't seen anyone suggest using Variable::Magic to achieve this. I'm thinking of writing a base class with a class method on the lines of this.
sub new { my $class = shift; my %params = @_; my $protected = sub { croak qq{Attempt to access protected data "$_[2]"} unless call +er->isa(__PACKAGE__); }; my $wiz = wizard( store => $protected, fetch => $protected, exists => $protected, delete => $protected, ); my %self; cast %self, $wiz; my $self = \%self; bless $self, $class; $self->$_($params{$_}) foreach keys %params; return $self; }
Does anyone have any views on whether this (a) will work correctly and (b) will be useful? The intention is to make the underlying hash inaccessable other than to subclasses of a class using this as a parent.
The main problem I'm trying to solve is the programmer who accidentally types $obj->{field} when he meant $obj->field, thereby inadvertantly bypassing any clever stuff in the getter.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Private & Protected Objects
by tobyink (Canon) on Aug 10, 2014 at 20:15 UTC | |
|
Re: Private & Protected Objects
by afoken (Chancellor) on Aug 10, 2014 at 18:25 UTC | |
by einhverfr (Friar) on Sep 05, 2014 at 02:52 UTC | |
|
Re: Private & Protected Objects (Variable::Magic)
by Anonymous Monk on Aug 10, 2014 at 20:05 UTC |