Debian Codegraph @

README

raw
1 NAME
2 Class::Default - Static calls apply to a default instantiation
3
4 SYNOPSIS
5 # Create the defaulted class
6 package Foo::Base;
7
8 use base 'Class::Default';
9
10 sub new { bless {}, $_[0] }
11
12 sub show {
13 my $self = shift->_self;
14 "$self";
15 }
16
17 # Do something to the default object
18
19 package main;
20
21 print Foo::Bar->show;
22
23 # Prints 'Foo::Bar=HASH(0x80d22f8)'
24
25 DESCRIPTION
26 Class::Default provides a mechanism to allow your class to take static
27 method calls and apply it to a default instantiation of an object. It
28 provides a flexibility to an API that allows it to be used more
29 confortably in different situations.
30
31 A good example of this technique in use is CGI.pm. When you use a static
32 method, like "CGI-"header>, your call is being applied to a default
33 instantiation of a CGI object.
34
35 This technique appears to be especially usefull when writing modules
36 that you want to be used in either a single use or a persistant
37 environment. In a CGI like environment, you want the simplicity of a
38 static interface. You can call "Class-"method> directly, without having
39 to pass an instantiation around constantly.
40
41 USING THE MODULES
42 Class::Default provides a couple of levels of control. They start with
43 simple enabling the method to apply to the default instantation, and
44 move on to providing some level of control over the creation of the
45 default object.
46
47 Inheriting from Class::Default
48 To start, you will need to inherit from Class::Default. You do this in
49 the normal manner, using something like "use base 'Class::Default'", or
50 setting the @ISA value directly. "Class::Default" does not have a
51 default constructor or any public methods, so you should be able to use
52 it a multiple inheritance situation without any implications.
53
54 Making method work
55 To make your class work with Class::Default you need to make a small
56 adjustment to each method that you would like to be able to access the
57 default object.
58
59 A typical method will look something like the following
60
61 sub foobar {
62 my $self = shift;
63
64 # Do whatever the method does
65 }
66
67 To make the method work with Class::Default, you should change it to the
68 following
69
70 sub foobar {
71 my $self = shift->_self;
72
73 # Do whatever the method does
74 }
75
76 This change is very low impact, easy to use, and will not make any other
77 differences to the way your code works.
78
79 Control over the default object
80 When needed, Class::Default will make a new instantation of your class
81 and cache it to be used whenever a static call is made. It does this in
82 the simplest way possible, by calling "Class-"new()> with no arguments.
83
84 This is fine if you have a very pure class that can handle creating a
85 new object without any arguments, but many classes expect some sort of
86 argument to the the constructor, and indeed that the constructor that
87 should be used it the "new" method.
88
89 Enter the "_create_default_object" method. By overloading the
90 "_create_default_object" method in your class, you can custom create the
91 default object. This will used to create the default object on demand,
92 the first time a method is called. For example, the following class
93 demonstrate the use of "_create_default_object" to set some values in
94 the default object.
95
96 package Slashdot::User;
97
98 use base 'Class::Default';
99
100 # Constructor
101 sub new {
102 my $class = shift;
103 my $name = shift;
104
105 my $self = {
106 name => $name,
107 favourite_color => '',
108 };
109
110 return bless $self, $class;
111 }
112
113 # Default constructor
114 sub _create_default_object {
115 my $class = shift;
116
117 my $self = $class->new( 'Anonymous Coward' );
118 $self->{favourite_color} = 'Orange';
119
120 return $self;
121 }
122
123 sub name {
124 $_[0]->_self->{name};
125 }
126
127 sub favourite_color {
128 $_[0]->_self->{favourite_color};
129 }
130
131 That provides a statically accessible default object that could be used
132 as in the following manner.
133
134 print "The default slashdot user is " . Slashdot::User->name
135 . " and they like the colour " . Slashdot::User->favourite_color;
136
137 Remember that the default object is persistant, so changes made to the
138 statically accessible object can be recovered later.
139
140 Getting access to the default object
141 There are a few ways to do this, but the easiest way is to simple do the
142 following
143
144 my $default = Slashdot::User->_get_default;
145
146 METHODS
147 _self
148 Used by methods to make the method apply to the default object if called
149 statically without affecting normal object methods.
150
151 _class
152 The "_class" method provides the opposite of the "_self" method. Instead
153 of always getting an object, "_class" will always get the class name, so
154 a method can be guarenteed to run in a static context. This is not
155 essential to the use of a "Class::Default" module, but is provided as a
156 convenience.
157
158 _get_default
159 Used to get the default object directly.
160
161 _create_default_object
162 To be overloaded by your class to set any properties to the default
163 object at creation time.
164
165 BUGS
166 No known bugs, but suggestions are welcome
167
168 SUPPORT
169 Bugs should be reported via the CPAN bug tracker at
170
171 <http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Class-Default>
172
173 For other issues, contact the author
174
175 AUTHOR
176 Adam Kennedy <[email protected]>
177
178 SEE ALSO
179 <http://ali.as/>, Class::Singleton
180
181 COPYRIGHT
182 Copyright (c) 2002 - 2006 Adam Kennedy.
183
184 This program is free software; you can redistribute it and/or modify it
185 under the same terms as Perl itself.
186
187 The full text of the license can be found in the LICENSE file included
188 with this module.
189