File Coverage

blib/lib/Class/Default.pm
Criterion Covered Total %
statement 22 22 100.0
branch 5 6 83.3
condition 3 6 50.0
subroutine 9 9 100.0
pod n/a
total 39 43 90.7


line stmt bran cond sub pod time code
1             package Class::Default;
2              
3             # The Class::Default package allows a class that inherits from
4             # it to act as both an instantiatable and static class.
5              
6             # See POD for more details.
7              
8 2     2   26347 use 5.005;
  2         7  
  2         71  
9 2     2   18 use strict;
  2         3  
  2         69  
10 2     2   10 use Carp ();
  2         12  
  2         35  
11              
12             # Define globals
13 2     2   11 use vars qw{$VERSION %DEFAULT};
  2         3  
  2         122  
14             BEGIN {
15 2     2   4 $VERSION = '1.51';
16              
17             # Create the default object storage.
18 2         331 %DEFAULT = ();
19             }
20              
21             # Get the default object if we are passed the class name.
22             sub _self {
23 2     2   503 my $either = shift;
24 2 100 33     16 ref($either) ? $either
25             : $DEFAULT{$either}
26             || ($DEFAULT{$either} = $either->_create_default_object)
27             || Carp::croak "Error while creating default object";
28             }
29              
30             # Suplimentary method to reliably get ONLY the class
31 2 100   2   519 sub _class { ref $_[0] or $_[0] }
32              
33             # Retrieve the default object for a class, either from
34             # the cache, or create it new.
35             sub _get_default {
36 2     2   4605 my $class = shift;
37 2 50 66     17 $DEFAULT{$class}
38             || ($DEFAULT{$class} = $class->_create_default_object)
39             || Carp::croak "Error while creating default object";
40             }
41              
42             # Creates the default object.
43             # Used to provide options to a constructor to create the default object.
44             sub _create_default_object {
45 1     1   3 my $class = shift;
46              
47             ### When you copy this to overload it, you should add
48             ### arguments to the constructor call as needed.
49              
50             # Create the new object.
51 1         4 my $self = $class->new;
52              
53             ### Make any modifications to the default object here
54              
55 1         17 $self;
56             }
57              
58             1;
59              
60             __END__
61              
62             =pod
63              
64             =head1 NAME
65              
66             Class::Default - Static calls apply to a default instantiation
67              
68             =head1 SYNOPSIS
69              
70             # Create the defaulted class
71             package Foo::Base;
72            
73             use base 'Class::Default';
74            
75             sub new { bless {}, $_[0] }
76            
77             sub show {
78             my $self = shift->_self;
79             "$self";
80             }
81            
82             # Do something to the default object
83            
84             package main;
85            
86             print Foo::Bar->show;
87            
88             # Prints 'Foo::Bar=HASH(0x80d22f8)'
89              
90             =head1 DESCRIPTION
91              
92             Class::Default provides a mechanism to allow your class to take static method
93             calls and apply it to a default instantiation of an object. It provides a
94             flexibility to an API that allows it to be used more confortably in
95             different situations.
96              
97             A good example of this technique in use is CGI.pm. When you use a static
98             method, like C<CGI->header>, your call is being applied to a default
99             instantiation of a CGI object.
100              
101             This technique appears to be especially usefull when writing modules that you
102             want to be used in either a single use or a persistant environment. In a CGI
103             like environment, you want the simplicity of a static interface. You can
104             call C<Class->method> directly, without having to pass an instantiation
105             around constantly.
106              
107             =head1 USING THE MODULES
108              
109             Class::Default provides a couple of levels of control. They start with simple
110             enabling the method to apply to the default instantation, and move on to
111             providing some level of control over the creation of the default object.
112              
113             =head2 Inheriting from Class::Default
114              
115             To start, you will need to inherit from Class::Default. You do this in the
116             normal manner, using something like C<use base 'Class::Default'>, or setting
117             the @ISA value directly. C<Class::Default> does not have a default
118             constructor or any public methods, so you should be able to use it a
119             multiple inheritance situation without any implications.
120              
121             =head2 Making method work
122              
123             To make your class work with Class::Default you need to make a small
124             adjustment to each method that you would like to be able to access the
125             default object.
126              
127             A typical method will look something like the following
128              
129             sub foobar {
130             my $self = shift;
131            
132             # Do whatever the method does
133             }
134              
135             To make the method work with Class::Default, you should change it to
136             the following
137              
138             sub foobar {
139             my $self = shift->_self;
140            
141             # Do whatever the method does
142             }
143              
144             This change is very low impact, easy to use, and will not make any other
145             differences to the way your code works.
146              
147             =head2 Control over the default object
148              
149             When needed, Class::Default will make a new instantation of your class
150             and cache it to be used whenever a static call is made. It does this in
151             the simplest way possible, by calling C<Class->new()> with no arguments.
152              
153             This is fine if you have a very pure class that can handle creating a
154             new object without any arguments, but many classes expect some sort of
155             argument to the the constructor, and indeed that the constructor that
156             should be used it the C<new> method.
157              
158             Enter the C<_create_default_object> method. By overloading the
159             C<_create_default_object> method in your class, you can custom create the
160             default object. This will used to create the default object on demand, the
161             first time a method is called. For example, the following class demonstrate
162             the use of C<_create_default_object> to set some values in the default
163             object.
164              
165             package Slashdot::User;
166            
167             use base 'Class::Default';
168            
169             # Constructor
170             sub new {
171             my $class = shift;
172             my $name = shift;
173            
174             my $self = {
175             name => $name,
176             favourite_color => '',
177             };
178            
179             return bless $self, $class;
180             }
181            
182             # Default constructor
183             sub _create_default_object {
184             my $class = shift;
185            
186             my $self = $class->new( 'Anonymous Coward' );
187             $self->{favourite_color} = 'Orange';
188            
189             return $self;
190             }
191            
192             sub name {
193             $_[0]->_self->{name};
194             }
195            
196             sub favourite_color {
197             $_[0]->_self->{favourite_color};
198             }
199              
200             That provides a statically accessible default object that could be used as in
201             the following manner.
202              
203             print "The default slashdot user is " . Slashdot::User->name
204             . " and they like the colour " . Slashdot::User->favourite_color;
205              
206             Remember that the default object is persistant, so changes made to the
207             statically accessible object can be recovered later.
208              
209             =head2 Getting access to the default object
210              
211             There are a few ways to do this, but the easiest way is to simple do
212             the following
213              
214             my $default = Slashdot::User->_get_default;
215              
216             =head1 METHODS
217              
218             =head2 _self
219              
220             Used by methods to make the method apply to the default object if called
221             statically without affecting normal object methods.
222              
223             =head2 _class
224              
225             The C<_class> method provides the opposite of the C<_self> method. Instead
226             of always getting an object, C<_class> will always get the class name, so
227             a method can be guarenteed to run in a static context. This is not
228             essential to the use of a C<Class::Default> module, but is provided as a
229             convenience.
230              
231             =head2 _get_default
232              
233             Used to get the default object directly.
234              
235             =head2 _create_default_object
236              
237             To be overloaded by your class to set any properties to the default
238             object at creation time.
239              
240             =head1 BUGS
241              
242             No known bugs, but suggestions are welcome
243              
244             =head1 SUPPORT
245              
246             Bugs should be reported via the CPAN bug tracker at
247              
248             L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Class-Default>
249              
250             For other issues, contact the author
251              
252             =head1 AUTHOR
253              
254             Adam Kennedy E<lt>adamk@cpan.orgE<gt>
255              
256             =head1 SEE ALSO
257              
258             L<http://ali.as/>, L<Class::Singleton>
259              
260             =head1 COPYRIGHT
261              
262             Copyright (c) 2002 - 2006 Adam Kennedy.
263              
264             This program is free software; you can redistribute
265             it and/or modify it under the same terms as Perl itself.
266              
267             The full text of the license can be found in the
268             LICENSE file included with this module.
269              
270             =cut