Thanks greatly to input from Joshua ben Jore, my new module Devel::EnforceEncapsulation is headed for CPAN. This module employs a hack to block raw access to a blessed reference from outside its class. An example:
use BankAccount;
use Devel::EnforceEncapsulation;
Devel::EnforceEncapsulation->apply_to( 'BankAccount' );
my $acct = BankAccount->new();
print $acct->balance(),"\n"; # ok
print $acct->{balance},"\n"; # dies
The original idea came from Ovid and Adrian Howard. Ovid has expressed an interest in creating a Class::Encapsulate which would be a production-quality version of this enforcement.
-apply_to( $obj )? (Score:1)
Re: (Score:1)
Devel::EnforceEncapsulation->apply_to($pkg);
my $obj = $pkg->new();
Devel::EnforceEncapsulation->remove_from($pkg);
The instance keeps the overload, but subsequent instantiations are not affected. The only downside to that approach is that it might blow away any deref overloads that may exist in $pkg. But that's a pretty small hazard.
Re: (Score:1)
Re: (Score:1)
Changing my mind (Score:2)
After giving it a lot of thought, I'm thinking now that Class::Encapsulate might seem like a good idea, but it's probably more trouble than it's worth. I think that your plan of applying it at runtime for development is probably a cleaner way to go.
Re: (Score:1)
In typical Linus style that statement is harsher than it need
deja vu, kind of... (Score:2)
Re: (Score:1)
The key difference between our implementations are:
* Mine applies externally post-facto, yours from inside the code
* Mine allows access from sub/superclasses, yours does not (deliberately!)
* Mine supports all of overload.pm's dereferencers, yours supports %,@,$, and &.
* Mine allows access from anything in the same package, yours allows access