PageRenderTime 90ms CodeModel.GetById 17ms RepoModel.GetById 1ms app.codeStats 0ms

/Class-InsideOut-1.10/lib/Class/InsideOut.pod

#
Unknown | 467 lines | 311 code | 156 blank | 0 comment | 0 complexity | 9073cd015c8b09b1b66b6fde9027403a MD5 | raw file
Possible License(s): Apache-2.0
  1. # Generated by Pod::WikiDoc version 0.18
  2. =pod
  3. =head1 NAME
  4. Class::InsideOut - a safe, simple inside-out object construction kit
  5. =head1 VERSION
  6. This documentation refers to version 1.10
  7. =head1 SYNOPSIS
  8. package My::Class;
  9. use Class::InsideOut qw( public readonly private register id );
  10. public name => my %name; # accessor: name()
  11. readonly ssn => my %ssn; # read-only accessor: ssn()
  12. private age => my %age; # no accessor
  13. sub new { register( shift ) }
  14. sub greeting {
  15. my $self = shift;
  16. return "Hello, my name is $name{ id $self }";
  17. }
  18. =head1 DESCRIPTION
  19. This is a simple, safe and streamlined toolkit for building inside-out objects.
  20. Unlike most other inside-out object building modules already on CPAN, this
  21. module aims for minimalism and robustness:
  22. =over
  23. =item *
  24. Does not require derived classes to subclass it
  25. =item *
  26. Uses no source filters, attributes or C<<< CHECK >>> blocks
  27. =item *
  28. Supports any underlying object type including black-box inheritance
  29. =item *
  30. Does not leak memory on object destruction
  31. =item *
  32. Overloading-safe
  33. =item *
  34. Thread-safe for Perl 5.8.5 or better
  35. =item *
  36. C<<< mod_perl >>> compatible
  37. =item *
  38. Makes no assumption about inheritance or initializer needs
  39. =back
  40. It provides the minimal support necessary for creating safe inside-out objects
  41. and generating flexible accessors.
  42. =head2 Additional documentation
  43. =over
  44. =item *
  45. L<Class::InsideOut::Manual::About> -- Guide to the inside-out
  46. technique, the C<<< Class::InsideOut >>> philosophy, and other inside-out
  47. implementations
  48. =item *
  49. L<Class::InsideOut::Manual::Advanced> -- Advanced topics including customizing
  50. accessors, black-box inheritance, serialization and thread safety
  51. =back
  52. =head1 USAGE
  53. =head2 Importing C<<< Class::InsideOut >>>
  54. C<<< Class::InsideOut >>> automatically imports several critical methods into the
  55. calling package, including C<<< DESTROY >>> and support methods for serializing
  56. objects with C<<< Storable >>>. These methods are intimately tied to correct
  57. functioning of inside-out objects and will always be imported regardless
  58. of whether additional functions are requested.
  59. Additional functions may be imported as usual by including them as arguments to
  60. C<<< use >>>. For example:
  61. use Class::InsideOut qw( register public );
  62. public name => my %name;
  63. sub new { register( shift ) }
  64. As a shortcut, C<<< Class::InsideOut >>> supports two tags for importing sets of
  65. functions:
  66. =over
  67. =item *
  68. C<<< :std >>> provides C<<< id >>>, C<<< private >>>, C<<< public >>>, C<<< readonly >>> and C<<< register >>>
  69. =item *
  70. C<<< :all >>> imports all functions (including an optional constructor)
  71. =back
  72. B<Note>: Automatic imports can be bypassed via C<<< require >>> or by passing an empty
  73. list to C<<< use Class::InsideOut >>>. There is almost no circumstance in which
  74. this is a good idea.
  75. =head2 Object properties and accessors
  76. Object properties are declared with the C<<< public >>>, C<<< readonly >>> and C<<< private >>>
  77. functions. They must be passed a label and the lexical hash that will be
  78. used to store object properties:
  79. public name => my %name;
  80. readonly ssn => my %ssn;
  81. private age => my %age;
  82. Properties for an object are accessed through an index into the lexical hash
  83. based on the memory address of the object. This memory address I<must> be
  84. obtained via C<<< Scalar::Util::refaddr >>>. The alias C<<< id >>> may be imported for
  85. brevity.
  86. $name{ refaddr $self } = "James";
  87. $ssn { id $self } = 123456789;
  88. $age { id $self } = 32;
  89. B<Tip>: since C<<< refaddr >>> and C<<< id >>> are function calls, it may be efficient to
  90. store the value once at the beginning of a method, particularly if it is being
  91. called repeatedly, e.g. within a loop.
  92. Object properties declared with C<<< public >>> will have an accessor created
  93. with the same name as the label. If the accessor is passed an argument, the
  94. property will be set to the argument. The accessor always returns the value of
  95. the property.
  96. # Outside the class
  97. $person = My::Class->new;
  98. $person->name( "Larry" );
  99. Object properties declared with C<<< readonly >>> will have a read-only accessor
  100. created. The accessor will die if passed an argument to set the property
  101. value. The property may be set directly in the hash from within the class
  102. package as usual.
  103. # Inside the class
  104. $ssn { id $person } = 987654321;
  105. # Inside or outside the class
  106. $person->ssn( 123456789 ); # dies
  107. Property accessors may also be hand-written by declaring the property
  108. C<<< private >>> and writing whatever style of accessor is desired. For example:
  109. sub age { $age{ id $_[0] } }
  110. sub set_age { $age{ id $_[0] } = $_[1] }
  111. Hand-written accessors will be very slightly faster as generated accessors hold
  112. a reference to the property hash rather than accessing the property hash
  113. directly.
  114. It is also possible to use a package hash instead of a lexical hash to store
  115. object properties:
  116. public name => our %name;
  117. However, this makes private object data accessable outside the class and incurs
  118. a slight performance penalty when accessing the property hash directly; it is
  119. not recommended to do this unless you really need it for some specialized
  120. reason.
  121. =head2 Object construction
  122. C<<< Class::InsideOut >>> provides no default constructor method as there are many
  123. possible ways of constructing an inside-out object. This avoids constraining
  124. users to any particular object initialization or superclass initialization
  125. methodology.
  126. By using the memory address of the object as the index for properties, I<any>
  127. type of reference may be used as the basis for an inside-out object with
  128. C<<< Class::InsideOut >>>.
  129. sub new {
  130. my $class = shift;
  131. my $self = \( my $scalar ); # anonymous scalar
  132. # my $self = {}; # anonymous hash
  133. # my $self = []; # anonymous array
  134. # open my $self, "<", $filename; # filehandle reference
  135. bless $self, $class;
  136. register( $self );
  137. }
  138. However, to ensure that the inside-out object is thread-safe, the C<<< register >>>
  139. function I<must> be called on the newly created object. The C<<< register >>>
  140. function may also be called with just the class name for the common
  141. case of blessing an anonymous scalar.
  142. register( $class ); # same as register( bless \(my $s), $class )
  143. As a convenience, C<<< Class::InsideOut >>> provides an optional C<<< new >>> constructor
  144. for simple objects. This constructor automatically initializes the object
  145. from keyE<sol>value pairs passed to the constructor for all keys matching the
  146. name of a property (including otherwise "private" or "readonly" properties).
  147. A more advanced technique for object construction uses another object, usually
  148. a superclass object, as the object reference. See "black-box inheritance" in
  149. L<Class::InsideOut::Manual::Advanced>.
  150. =head2 Object destruction
  151. C<<< Class::InsideOut >>> automatically exports a special C<<< DESTROY >>> function.
  152. This function cleans up object property memory for all declared properties the
  153. class and for all C<<< Class::InsideOut >>> based classes in the C<<< @ISA >>> array to
  154. avoid memory leaks or data collision.
  155. Additionally, if a user-supplied C<<< DEMOLISH >>> function is available in the same
  156. package, it will be called with the object being destroyed as its argument.
  157. C<<< DEMOLISH >>> can be used for custom destruction behavior such as updating class
  158. properties, closing sockets or closing database connections. Object properties
  159. will not be deleted until after C<<< DEMOLISH >>> returns.
  160. # Sample DEMOLISH: Count objects demolished (for whatever reason)
  161. my $objects_destroyed;
  162. sub DEMOLISH {
  163. $objects_destroyed++;
  164. }
  165. C<<< DEMOLISH >>> will only be called if it exists for an object's actual
  166. class. C<<< DEMOLISH >>> will not be inherited and C<<< DEMOLISH >>> will not be called
  167. automatically for any superclasses.
  168. C<<< DEMOLISH >>> should manage any necessary calls to superclass C<<< DEMOLISH >>>
  169. methods. As with C<<< new >>>, implementation details are left to the user based on
  170. the user's approach to object inheritance. Depending on how the inheritance
  171. chain is constructed and how C<<< DEMOLISH >>> is being used, users may wish to
  172. entirely override superclass C<<< DEMOLISH >>> methods, rely upon C<<< SUPER::DEMOLISH >>>,
  173. or may prefer to walk the entire C<<< @ISA >>> tree:
  174. use Class::ISA;
  175. sub DEMOLISH {
  176. my $self = shift;
  177. # class specific demolish actions
  178. # DEMOLISH for all parent classes, but only once
  179. my @parents = Class::ISA::super_path( __PACKAGE__ );
  180. my %called;
  181. for my $p ( @parents ) {
  182. my $demolish = $p->can('DEMOLISH');
  183. $demolish->($self) if not $called{ $demolish }++;
  184. }
  185. }
  186. =head1 FUNCTIONS
  187. =head2 C<<< id >>>
  188. $name{ id $object } = "Larry";
  189. This is a shorter, mnemonic alias for C<<< Scalar::Util::refaddr >>>. It returns the
  190. memory address of an object (just like C<<< refaddr >>>) as the index to access
  191. the properties of an inside-out object.
  192. =head2 C<<< new >>>
  193. My::Class->new( name => "Larry", age => 42 );
  194. This simplistic constructor is provided as a convenience and is only exported
  195. on request. When called as a class method, it returns a blessed anonymous
  196. scalar. Arguments will be used to initialize all matching inside-out class
  197. properties in the C<<< @ISA >>> tree. The argument may be a hash or hash reference.
  198. Note: Properties are set directly, not via accessors. This means C<<< set_hook >>>
  199. functions will not be called. For more robust argument checking, you will
  200. need to implement your own constructor.
  201. =head2 C<<< options >>>
  202. Class::InsideOut::options( \%new_options );
  203. %current_options = Class::InsideOut::options();
  204. The C<<< options >>> function sets default options for use with all subsquent property
  205. definitions for the calling package. If called without arguments, this
  206. function will return the options currently in effect. When called with a hash
  207. reference of options, these will be joined with the existing defaults,
  208. overriding any options of the same name.
  209. =head2 C<<< private >>>
  210. private weight => my %weight;
  211. private haircolor => my %hair_color, { %options };
  212. This is an alias to C<<< property >>> that also sets the privacy option to 'private'.
  213. It will override default options or options passed as an argument.
  214. =head2 C<<< property >>>
  215. property name => my %name;
  216. property rank => my %rank, { %options };
  217. Declares an inside-out property. Two arguments are required and a third is
  218. optional. The first is a label for the property; this label will be used for
  219. introspection and generating accessors and thus must be a valid perl
  220. identifier. The second argument must be the lexical hash that will be used to
  221. store data for that property. Note that the C<<< my >>> keyword can be included as
  222. part of the argument rather than as a separate statement. The property will be
  223. tracked for memory cleanup during object destruction and for proper
  224. thread-safety.
  225. If a third, optional argument is provided, it must be a reference to a hash
  226. of options that will be applied to the property and will override any
  227. default options that have been set.
  228. =head2 C<<< public >>>
  229. public height => my %height;
  230. public age => my %age, { %options };
  231. This is an alias to C<<< property >>> that also sets the privacy option to 'public'.
  232. It will override default options or options passed as an argument.
  233. =head2 C<<< readonly >>>
  234. readonly ssn => my %ssn;
  235. readonly fingerprint => my %fingerprint, { %options };
  236. This is an alias to C<<< property >>> that sets the privacy option to 'public' and
  237. adds a C<<< set_hook >>> option that dies if an attempt is made to use the accessor to
  238. change the property. It will override default options or options passed as an
  239. argument.
  240. =head2 C<<< register >>>
  241. register( bless( $object, $class ) ); # register the object
  242. register( $reference, $class ); # automatic bless
  243. register( $class ); # automatic blessed scalar
  244. Registers objects for thread-safety. This should be called as part of a
  245. constructor on a object blessed into the current package. Returns the
  246. resulting object. When called with only a class name, C<<< register >>> will bless an
  247. anonymous scalar reference into the given class. When called with both a
  248. reference and a class name, C<<< register >>> will bless the reference into the class.
  249. =head1 OPTIONS
  250. Options customize how properties are generated. Options may be set as a
  251. default with the C<<< options >>> function or passed as a hash reference to
  252. C<<< public >>>, C<<< private >>> or C<<< property >>>.
  253. Valid options include:
  254. =head2 C<<< privacy >>>
  255. property rank => my %rank, { privacy => 'public' };
  256. property serial => my %serial, { privacy => 'private' };
  257. If the I<privacy> option is set to I<public>, an accessor will be created
  258. with the same name as the label. If the accessor is passed an argument, the
  259. property will be set to the argument. The accessor always returns the value of
  260. the property.
  261. =head2 C<<< get_hook >>>
  262. public list => my %list, {
  263. get_hook => sub { @$_ }
  264. };
  265. Defines an accessor hook for when values are retrieved. C<<< $_ >>> is locally
  266. aliased to the property value for the object. I<The return value of the hook is
  267. passed through as the return value of the accessor.> See "Customizing Accessors"
  268. in L<Class::InsideOut::Manual::Advanced> for details.
  269. =head2 C<<< set_hook >>>
  270. public age => my %age, {
  271. set_hook => sub { /^\d+$/ or die "must be an integer" }
  272. };
  273. Defines an accessor hook for when values are set. The hook subroutine receives
  274. the entire argument list. C<<< $_ >>> is locally aliased to the first argument for
  275. convenience. The property receives the value of C<<< $_ >>>. See "Customizing
  276. Accessors" in L<Class::InsideOut::Manual::Advanced> for details.
  277. =head1 SEE ALSO
  278. Programmers seeking a more full-featured approach to inside-out objects are
  279. encouraged to explore L<Object::InsideOut>. Other implementations are also
  280. noted in L<Class::InsideOut::Manual::About>.
  281. =head1 KNOWN LIMITATIONS
  282. Requires weak reference support (Perl E<gt>= 5.6) and Scalar::Util::weaken() to
  283. avoid memory leaks and to provide thread-safety.
  284. =head1 ROADMAP
  285. Features slated for after the 1.0 release include:
  286. =over
  287. =item *
  288. Adding support for L<Data::Dump::Streamer> serialization hooks
  289. =item *
  290. Adding additional accessor styles (e.g. get_name()E<sol>set_name())
  291. =item *
  292. Further documentation revisions and clarification
  293. =back
  294. =head1 BUGS
  295. Please report bugs or feature requests using the CPAN Request Tracker:
  296. L<http://rt.cpan.org/Public/Dist/Display.html?Name=Class-InsideOut>
  297. When submitting a bug or request, please include a test-file or a patch to an
  298. existing test-file that illustrates the bug or desired feature.
  299. =head1 AUTHOR
  300. David A. Golden (DAGOLDEN)
  301. =head1 COPYRIGHT AND LICENSE
  302. Copyright (c) 2006, 2007 by David A. Golden
  303. Licensed under the Apache License, Version 2.0 (the "License");
  304. you may not use this file except in compliance with the License.
  305. You may obtain a copy of the License at
  306. LE<lt>http:E<sol>E<sol>www.apache.orgE<sol>licensesE<sol>LICENSE-2.0E<gt>
  307. Unless required by applicable law or agreed to in writing, software
  308. distributed under the License is distributed on an "AS IS" BASIS,
  309. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  310. See the License for the specific language governing permissions and
  311. limitations under the License.