diff options
author | Dave Rolsky <autarch@urth.org> | 2011-07-07 10:47:35 -0500 |
---|---|---|
committer | Dave Rolsky <autarch@urth.org> | 2011-09-08 21:47:23 -0500 |
commit | e011bc3422f7051949dc1f88e66f2eaed8d46348 (patch) | |
tree | ba07ca47bfc376850003b684a2a52cb196b07f8c /pod/perlobj.pod | |
parent | af36000c88ef5604a340513b05466c8690459612 (diff) | |
download | perl-e011bc3422f7051949dc1f88e66f2eaed8d46348.tar.gz |
Add more details on inside-out objects from David Golden, and an inside-out class example from Abigail.
Add strict & warning to blessed scalar class example
Diffstat (limited to 'pod/perlobj.pod')
-rw-r--r-- | pod/perlobj.pod | 47 |
1 files changed, 40 insertions, 7 deletions
diff --git a/pod/perlobj.pod b/pod/perlobj.pod index da27c5ad59..7f45d8eed1 100644 --- a/pod/perlobj.pod +++ b/pod/perlobj.pod @@ -799,6 +799,9 @@ Here's an example of a module as a blessed scalar: package Time; + use strict; + use warnings; + sub new { my $class = shift; @@ -816,17 +819,47 @@ Here's an example of a module as a blessed scalar: =head2 Inside-Out objects -The Perl community in the past has experimented with a technique -referred to as "inside-out objects". Without going into the details, -this was a method of enforcing data hiding for an object. An inside-out -object stores its data in a lexical class variable rather than in the -object itself. +In the past, the Perl community experimented with a technique called +"inside-out objects". An inside-out object stores its data in a lexical +class variable, indexed on a unique property of the object, such as its +memory address, rather than in the object itself. -This technique was popular for a while, and was recommended in Damian -Conway's Perl Best Practices. The L<Object::InsideOut> module on CPAN +This technique was popular for a while (and was recommended in Damian +Conway's I<Perl Best Practices>), but never achieved wide adoption due +to additional complexity. The L<Object::InsideOut> module on CPAN provides a comprehensive implementation of this technique, and you may see it or other inside-out modules in the wild. +Here is a simple example of the technique, using the +L<Hash::Util::FieldHash> core module. This module was added to the core +to support inside-out object implementations. + + package Time::InsideOut; + + use strict; + use warnings; + + use Hash::Util::FieldHash 'fieldhash'; + + fieldhash my %TIME; + + sub new { + my $class = shift; + my $self = bless \( my $empty ), $class; + $TIME{$self} = time; + + $self; + } + + sub epoch { + my $self = shift; + + $TIME{$self}; + } + + my $time = Time::InsideOut->new; + print $time->epoch; + =head2 Pseudo-hashes The pseudo-hash feature was an experimental feature introduced in |