1 .\" Automatically generated by Pod::Man 2.22 (Pod::Simple 3.10)
4 .\" ========================================================================
5 .de Sp \" Vertical space (when we can't use .PP)
9 .de Vb \" Begin verbatim text
14 .de Ve \" End verbatim text
18 .\" Set up some character translations and predefined strings. \*(-- will
19 .\" give an unbreakable dash, \*(PI will give pi, \*(L" will give a left
20 .\" double quote, and \*(R" will give a right double quote. \*(C+ will
21 .\" give a nicer C++. Capital omega is used to do unbreakable dashes and
22 .\" therefore won't be available. \*(C` and \*(C' expand to `' in nroff,
23 .\" nothing in troff, for use with C<>.
25 .ds C+ C\v'-.1v'\h'-1p'\s-2+\h'-1p'+\s0\v'.1v'\h'-1p'
29 . if (\n(.H=4u)&(1m=24u) .ds -- \(*W\h'-12u'\(*W\h'-12u'-\" diablo 10 pitch
30 . if (\n(.H=4u)&(1m=20u) .ds -- \(*W\h'-12u'\(*W\h'-8u'-\" diablo 12 pitch
43 .\" Escape single quotes in literal strings from groff's Unicode transform.
47 .\" If the F register is turned on, we'll generate index entries on stderr for
48 .\" titles (.TH), headers (.SH), subsections (.SS), items (.Ip), and index
49 .\" entries marked with X<> in POD. Of course, you'll have to process the
50 .\" output yourself in some meaningful fashion.
53 . tm Index:\\$1\t\\n%\t"\\$2"
63 .\" Accent mark definitions (@(#)ms.acc 1.5 88/02/08 SMI; from UCB 4.2).
64 .\" Fear. Run. Save yourself. No user-serviceable parts.
65 . \" fudge factors for nroff and troff
74 . ds #H ((1u-(\\\\n(.fu%2u))*.13m)
80 . \" simple accents for nroff and troff
90 . ds ' \\k:\h'-(\\n(.wu*8/10-\*(#H)'\'\h"|\\n:u"
91 . ds ` \\k:\h'-(\\n(.wu*8/10-\*(#H)'\`\h'|\\n:u'
92 . ds ^ \\k:\h'-(\\n(.wu*10/11-\*(#H)'^\h'|\\n:u'
93 . ds , \\k:\h'-(\\n(.wu*8/10)',\h'|\\n:u'
94 . ds ~ \\k:\h'-(\\n(.wu-\*(#H-.1m)'~\h'|\\n:u'
95 . ds / \\k:\h'-(\\n(.wu*8/10-\*(#H)'\z\(sl\h'|\\n:u'
97 . \" troff and (daisy-wheel) nroff accents
98 .ds : \\k:\h'-(\\n(.wu*8/10-\*(#H+.1m+\*(#F)'\v'-\*(#V'\z.\h'.2m+\*(#F'.\h'|\\n:u'\v'\*(#V'
99 .ds 8 \h'\*(#H'\(*b\h'-\*(#H'
100 .ds o \\k:\h'-(\\n(.wu+\w'\(de'u-\*(#H)/2u'\v'-.3n'\*(#[\z\(de\v'.3n'\h'|\\n:u'\*(#]
101 .ds d- \h'\*(#H'\(pd\h'-\w'~'u'\v'-.25m'\f2\(hy\fP\v'.25m'\h'-\*(#H'
102 .ds D- D\\k:\h'-\w'D'u'\v'-.11m'\z\(hy\v'.11m'\h'|\\n:u'
103 .ds th \*(#[\v'.3m'\s+1I\s-1\v'-.3m'\h'-(\w'I'u*2/3)'\s-1o\s+1\*(#]
104 .ds Th \*(#[\s+2I\s-2\h'-\w'I'u*3/5'\v'-.3m'o\v'.3m'\*(#]
105 .ds ae a\h'-(\w'a'u*4/10)'e
106 .ds Ae A\h'-(\w'A'u*4/10)'E
107 . \" corrections for vroff
108 .if v .ds ~ \\k:\h'-(\\n(.wu*9/10-\*(#H)'\s-2\u~\d\s+2\h'|\\n:u'
109 .if v .ds ^ \\k:\h'-(\\n(.wu*10/11-\*(#H)'\v'-.4m'^\v'.4m'\h'|\\n:u'
110 . \" for low resolution devices (crt and lpr)
111 .if \n(.H>23 .if \n(.V>19 \
124 .\" ========================================================================
126 .IX Title "Class::Singleton 3"
127 .TH Class::Singleton 3 "2007-09-28" "perl v5.8.7" "User Contributed Perl Documentation"
128 .\" For nroff, turn off justification. Always turn off hyphenation; it makes
129 .\" way too many mistakes in technical documents.
133 Class::Singleton \- Implementation of a "Singleton" class
135 .IX Header "SYNOPSIS"
137 \& use Class::Singleton;
139 \& my $one = Class::Singleton\->instance(); # returns a new instance
140 \& my $two = Class::Singleton\->instance(); # returns same instance
143 .IX Header "DESCRIPTION"
144 This is the \f(CW\*(C`Class::Singleton\*(C'\fR module. A Singleton describes an object class
145 that can have only one instance in any system. An example of a Singleton
146 might be a print spooler or system registry. This module implements a
147 Singleton class from which other classes can be derived. By itself, the
148 \&\f(CW\*(C`Class::Singleton\*(C'\fR module does very little other than manage the instantiation
149 of a single object. In deriving a class from \f(CW\*(C`Class::Singleton\*(C'\fR, your module
150 will inherit the Singleton instantiation method and can implement whatever
151 specific functionality is required.
153 For a description and discussion of the Singleton class, see
154 \&\*(L"Design Patterns\*(R", Gamma et al, Addison-Wesley, 1995, \s-1ISBN\s0 0\-201\-63361\-2.
156 .IX Header "PREREQUISITES"
157 \&\f(CW\*(C`Class::Singleton\*(C'\fR requires Perl version 5.004 or later. If you have an older
158 version of Perl, please upgrade to latest version, available from your nearest
159 \&\s-1CPAN\s0 site (see \s-1INSTALLATION\s0 below).
161 .IX Header "INSTALLATION"
162 The \f(CW\*(C`Class::Singleton\*(C'\fR module is available from \s-1CPAN\s0. As the 'perlmod' man
166 \& CPAN stands for the Comprehensive Perl Archive Network.
167 \& This is a globally replicated collection of all known Perl
168 \& materials, including hundreds of unbunded modules.
172 \& For an up\-to\-date listing of CPAN sites, see
173 \& http://www.perl.com/perl/ or ftp://ftp.perl.com/perl/ .
176 The module is available in the following directories:
179 \& /modules/by\-module/Class/Class\-Singleton\-<version>.tar.gz
180 \& /authors/id/ABW/Class\-Singleton\-<version>.tar.gz
183 \&\f(CW\*(C`Class::Singleton\*(C'\fR is distributed as a single gzipped tar archive file:
186 \& Class\-Singleton\-<version>.tar.gz
189 Note that \*(L"<version>\*(R" represents the current version number, of the
190 form "\f(CW1.23\fR". See \s-1VERSION\s0 below to determine the current version
191 number for \f(CW\*(C`Class::Singleton\*(C'\fR.
193 Unpack the archive to create an installation directory:
196 \& gunzip Class\-Singleton\-<version>.tar.gz
197 \& tar xvf Class\-Singleton\-<version>.tar
200 \&'cd' into that directory, make, test and install the module:
203 \& cd Class\-Singleton\-<version>
210 The '\f(CW\*(C`make install\*(C'\fR' will install the module on your system. You may need
211 root access to perform this task. If you install the module in a local
212 directory (for example, by executing "\f(CW\*(C`perl Makefile.PL LIB=~/lib\*(C'\fR" in the
213 above \- see \f(CW\*(C`perldoc MakeMaker\*(C'\fR for full details), you will need to ensure
214 that the \f(CW\*(C`PERL5LIB\*(C'\fR environment variable is set to include the location, or
215 add a line to your scripts explicitly naming the library location:
218 \& use lib \*(Aq/local/path/to/lib\*(Aq;
220 .SH "USING THE CLASS::SINGLETON MODULE"
221 .IX Header "USING THE CLASS::SINGLETON MODULE"
222 To import and use the \f(CW\*(C`Class::Singleton\*(C'\fR module the following line should
223 appear in your Perl program:
226 \& use Class::Singleton;
229 The \fIinstance()\fR method is used to create a new \f(CW\*(C`Class::Singleton\*(C'\fR instance,
230 or return a reference to an existing instance. Using this method, it is only
231 possible to have a single instance of the class in any system.
234 \& my $highlander = Class::Singleton\->instance();
237 Assuming that no \f(CW\*(C`Class::Singleton\*(C'\fR object currently exists, this first call
238 to \fIinstance()\fR will create a new \f(CW\*(C`Class::Singleton\*(C'\fR and return a reference
239 to it. Future invocations of \fIinstance()\fR will return the same reference.
242 \& my $macleod = Class::Singleton\->instance();
245 In the above example, both \f(CW$highlander\fR and \f(CW$macleod\fR contain the same
246 reference to a \f(CW\*(C`Class::Singleton\*(C'\fR instance. There can be only one.
247 .SH "DERIVING SINGLETON CLASSES"
248 .IX Header "DERIVING SINGLETON CLASSES"
249 A module class may be derived from \f(CW\*(C`Class::Singleton\*(C'\fR and will inherit the
250 \&\fIinstance()\fR method that correctly instantiates only one object.
253 \& package PrintSpooler;
254 \& use base \*(AqClass::Singleton\*(Aq;
256 \& # derived class specific code
266 The \f(CW\*(C`PrintSpooler\*(C'\fR class defined above could be used as follows:
271 \& my $spooler = PrintSpooler\->instance();
273 \& $spooler\->submit_job(...);
276 The \fIinstance()\fR method calls the \fI_new_instance()\fR constructor method the
277 first and only time a new instance is created. All parameters passed to the
278 \&\fIinstance()\fR method are forwarded to \fI_new_instance()\fR. In the base class
279 the \fI_new_instance()\fR method returns a blessed reference to a hash array
280 containing any arguments passed as either a hash reference or list of named
285 \& use base \*(AqClass::Singleton\*(Aq;
297 \& # either: hash reference of named parameters
298 \& my $config = MyConfig\->instance({ foo => 10, bar => 20 });
300 \& # or: list of named parameters
301 \& my $config = MyConfig\->instance( foo => 10, bar => 20 );
303 \& print $config\->foo(); # 10
304 \& print $config\->bar(); # 20
307 Derived classes may redefine the \fI_new_instance()\fR method to provide more
308 specific object initialisation or change the underlying object type (to a list
309 reference, for example).
312 \& package MyApp::Database;
313 \& use base \*(AqClass::Singleton\*(Aq;
316 \& # this only gets called the first time instance() is called
317 \& sub _new_instance {
318 \& my $class = shift;
319 \& my $self = bless { }, $class;
320 \& my $db = shift || "myappdb";
321 \& my $host = shift || "localhost";
323 \& $self\->{ DB } = DBI\->connect("DBI:mSQL:$db:$host")
324 \& || die "Cannot connect to database: $DBI::errstr";
326 \& # any other initialisation...
332 The above example might be used as follows:
335 \& use MyApp::Database;
337 \& # first use \- database gets initialised
338 \& my $database = MyApp::Database\->instance();
341 Some time later on in a module far, far away...
344 \& package MyApp::FooBar
345 \& use MyApp::Database;
347 \& # this FooBar object needs access to the database; the Singleton
348 \& # approach gives a nice wrapper around global variables.
351 \& my $class = shift;
353 \& database => MyApp::Database\->instance(),
358 The \f(CW\*(C`Class::Singleton\*(C'\fR \fIinstance()\fR method uses a package variable to store
359 a reference to any existing instance of the object. This variable,
360 "\f(CW\*(C`_instance\*(C'\fR", is coerced into the derived class package rather than the base
363 Thus, in the \f(CW\*(C`MyApp::Database\*(C'\fR example above, the instance variable would
367 \& $MyApp::Database::_instance;
370 This allows different classes to be derived from \f(CW\*(C`Class::Singleton\*(C'\fR that can
371 co-exist in the same system, while still allowing only one instance of any one
372 class to exists. For example, it would be possible to derive both
373 \&'\f(CW\*(C`PrintSpooler\*(C'\fR' and '\f(CW\*(C`MyApp::Database\*(C'\fR' from \f(CW\*(C`Class::Singleton\*(C'\fR and have a
374 single instance of \fIeach\fR in a system, rather than a single instance of
377 You can use the \fIhas_instance()\fR method to find out if a particular class
378 already has an instance defined. A reference to the instance is returned or
379 \&\f(CW\*(C`undef\*(C'\fR if none is currently defined.
382 \& my $instance = MyApp::Database\->has_instance()
383 \& || warn "No instance is defined yet";
387 .SS "\fIinstance()\fP"
388 .IX Subsection "instance()"
389 This method is called to return a current object instance or create a new
390 one by calling \fI_new_instance()\fR.
391 .SS "\fIhas_instance()\fP"
392 .IX Subsection "has_instance()"
393 This method returns a reference to any existing instance or \f(CW\*(C`undef\*(C'\fR if none
397 \& my $testing = MySingleton1\->has_instance()
398 \& || warn "No instance defined for MySingleton1";
400 .SS "\fI_new_instance()\fP"
401 .IX Subsection "_new_instance()"
402 This \*(L"private\*(R" method is called by \fIinstance()\fR to create a new object
403 instance if one doesn't already exist. It is not intended to be called
404 directly (although there's nothing to stop you from calling it if you're
405 really determined to do so).
407 It creates a blessed hash reference containing any arguments passed to the
408 method as either a hash reference or list of named parameters.
411 \& # either: hash reference of named parameters
412 \& my $example1 = MySingleton1\->new({ pi => 3.14, e => 2.718 });
414 \& # or: list of named parameters
415 \& my $example2 = MySingleton2\->new( pi => 3.14, e => 2.718 );
418 It is important to remember that the \fIinstance()\fR method will \fIonly\fR call
419 the \fI\fI_new_instance()\fI\fR method once, so any arguments you pass may be silently
420 ignored if an instance already exists. You can use the \fIhas_instance()\fR
421 method to determine if an instance is already defined.
424 Andy Wardley <abw@wardley.org> <http://wardley.org/>
426 Thanks to Andreas Koenig for providing some significant speedup patches and
430 This is version 1.4, released September 2007
432 .IX Header "COPYRIGHT"
433 Copyright Andy Wardley 1998\-2007. All Rights Reserved.
435 This module is free software; you can redistribute it and/or
436 modify it under the same terms as Perl itself.