1Devel::EnforceEncapsulaUtsieorn(C3o)ntributed Perl DocumDeenvtealt:i:oEnnforceEncapsulation(3)
2
3
4
6 Devel::EnforceEncapsulation - Find access violations to blessed objects
7
9 package BankAccount;
10 sub new {
11 my $pkg = shift;
12 return bless {}, $pkg;
13 }
14 sub balance {
15 my $self = shift;
16 return $self->{balance};
17 }
18 # ... etc. ...
19
20 package main;
21 Devel::EnforceEncapsulation->apply_to('BankAccount');
22 my $acct = BankAccount->new();
23 print $acct->balance(),"\n"; # ok
24 print $acct->{balance},"\n"; # dies
25
27 Copyright 2006 Clotho Advanced Media, Inc., <cpan@clotho.com>
28
29 This library is free software; you can redistribute it and/or modify it
30 under the same terms as Perl itself.
31
33 Encapsulation is the practice of creating subroutines to access the
34 properties of a class instead of accessing those properties directly.
35 The advantage of good encapsulation is that the author is permitted to
36 change the internal implementation of a class without breaking its
37 usage.
38
39 Object-oriented programming in Perl is most commonly implemented via
40 blessed hashes. This practice makes it easy for users of a class to
41 violate encapsulation by simply accessing the hash values directly.
42 Although less common, the same applies to classes implemented via
43 blessed arrays, scalars, filehandles, etc.
44
45 This module is a hack to block those direct accesses. If you try to
46 access a hash value of an object from it's own class, or a superclass
47 or subclass, all goes well. If you try to access a hash value from any
48 other package, an exception is thrown. The same applies to the scalar
49 value of a blessed scalar, entry in a blessed array, etc.
50
51 To be clear: this class is NOT intended for strict enforcement of
52 encapsulation. If you want bullet-proof encapsulation, use inside-out
53 objects or the like. Instead, this module is intended to be a
54 development or debugging aid in catching places where direct access is
55 used against classes implemented as blessed hashes.
56
57 To repeat: the encapsulation enforced here is a hack and is easily
58 circumvented. Please use this module for good (finding bugs), not evil
59 (making life harder for downstream developers).
60
62 $pkg->apply_to($other_pkg);
63 $pkg->apply_to($other_pkg, {policy => 'carp'});
64 Add strict encapsulation to an existing $other_pkg. The
65 encapsulation changes only apply to instances created after the
66 call.
67
68 The optional "policy" argument allows you to change the response to
69 an illegal access. By default the policy is "croak", which invokes
70 "Carp::croak()". The alternative is "carp" which invokes
71 "Carp::carp()" instead.
72
73 $pkg->remove_from($other_pkg);
74 Remove any encapsulation previously set by "apply_to()". This does
75 not affect instances created before this call.
76
78 Class::Privacy
79 Class::Privacy is a very similar implementation that is, in general,
80 stricter than this module. The key differences:
81
82 · D::E applies externally post-facto; C::P from inside the code.
83
84 · D::E allows access from sub/superclasses; C::P does not
85 (deliberately!).
86
87 · D::E supports all dereferencers from overload.pm; C::P supports
88 "%", "@", "$", and "&".
89
90 · D::E allows access from anything in the same package; C::P allows
91 access only from matching package AND file.
92
93 Inside-out classes
94 Class::InsideOut, Object::InsideOut and Class::Std are all
95 implementations of "inside-out" objects, which offer a stricter
96 encapsulation style at the expense of a less familiar coding style.
97
99 "Illegal attempt to access %s internals from %s"
100 (Fatal) You tried to access a hash property directly instead of via
101 an accessor method. The %s values are the object and caller
102 packages respectively.
103
105 We care about code quality. This distribution complies with the
106 following quality metrics:
107
108 · Perl::Critic v0.20 passes
109
110 · Devel::Cover test coverage at 100%
111
112 · Pod::Coverage at 100%
113
114 · Test::Spelling passes
115
116 · Test::Portability::Files passes
117
118 · Test::Kwalitee passes
119
121 The regression tests do not cover blessed code or glob references.
122
123 Any other issues:
124 <http://rt.cpan.org/Dist/Display.html?Queue=Devel-EnforceEncapsulation>
125
127 Clotho Advanced Media Inc., cpan@clotho.com
128
129 Maintainer: Chris Dolan
130
132 This idea, and the original source code, came from Adrian Howard via
133 Curtis "Ovid" Poe on
134 <http://www.perlmonks.org/?node_id=576707|perlmonks.org>. Adrian has
135 authorized me to release a variant of his code under the Perl license.
136
137 Joshua ben Jore provided crucial improvements that simplified yet
138 generalized the implementation. This module would not be half as
139 useful without his contributions.
140 <http://use.perl.org/comments.pl?sid=33253&cid=50863>
141
142 Ricardo Signes contributed the "{policy =" 'carp'}> feature (with
143 tests!). <http://rt.cpan.org/Ticket/Display.html?id=22024>
144
145
146
147perl v5.16.3 2006-10-11 Devel::EnforceEncapsulation(3)