.\" Automatically generated by Pod::Man 4.14 (Pod::Simple 3.40) .\" .\" Standard preamble: .\" ======================================================================== .de Sp \" Vertical space (when we can't use .PP) .if t .sp .5v .if n .sp .. .de Vb \" Begin verbatim text .ft CW .nf .ne \\$1 .. .de Ve \" End verbatim text .ft R .fi .. .\" Set up some character translations and predefined strings. \*(-- will .\" give an unbreakable dash, \*(PI will give pi, \*(L" will give a left .\" double quote, and \*(R" will give a right double quote. \*(C+ will .\" give a nicer C++. Capital omega is used to do unbreakable dashes and .\" therefore won't be available. \*(C` and \*(C' expand to `' in nroff, .\" nothing in troff, for use with C<>. .tr \(*W- .ds C+ C\v'-.1v'\h'-1p'\s-2+\h'-1p'+\s0\v'.1v'\h'-1p' .ie n \{\ . ds -- \(*W- . ds PI pi . if (\n(.H=4u)&(1m=24u) .ds -- \(*W\h'-12u'\(*W\h'-12u'-\" diablo 10 pitch . if (\n(.H=4u)&(1m=20u) .ds -- \(*W\h'-12u'\(*W\h'-8u'-\" diablo 12 pitch . ds L" "" . ds R" "" . ds C` "" . ds C' "" 'br\} .el\{\ . ds -- \|\(em\| . ds PI \(*p . ds L" `` . ds R" '' . ds C` . ds C' 'br\} .\" .\" Escape single quotes in literal strings from groff's Unicode transform. .ie \n(.g .ds Aq \(aq .el .ds Aq ' .\" .\" If the F register is >0, we'll generate index entries on stderr for .\" titles (.TH), headers (.SH), subsections (.SS), items (.Ip), and index .\" entries marked with X<> in POD. Of course, you'll have to process the .\" output yourself in some meaningful fashion. .\" .\" Avoid warning from groff about undefined register 'F'. .de IX .. .nr rF 0 .if \n(.g .if rF .nr rF 1 .if (\n(rF:(\n(.g==0)) \{\ . if \nF \{\ . de IX . tm Index:\\$1\t\\n%\t"\\$2" .. . if !\nF==2 \{\ . nr % 0 . nr F 2 . \} . \} .\} .rr rF .\" ======================================================================== .\" .IX Title "Class::Singleton 3pm" .TH Class::Singleton 3pm "2020-12-05" "perl v5.32.0" "User Contributed Perl Documentation" .\" For nroff, turn off justification. Always turn off hyphenation; it makes .\" way too many mistakes in technical documents. .if n .ad l .nh .SH "NAME" Class::Singleton \- Implementation of a "Singleton" class .SH "SYNOPSIS" .IX Header "SYNOPSIS" .Vb 1 \& use Class::Singleton; \& \& my $one = Class::Singleton\->instance(); # returns a new instance \& my $two = Class::Singleton\->instance(); # returns same instance .Ve .SH "DESCRIPTION" .IX Header "DESCRIPTION" This is the \f(CW\*(C`Class::Singleton\*(C'\fR module. A Singleton describes an object class that can have only one instance in any system. An example of a Singleton might be a print spooler or system registry. This module implements a Singleton class from which other classes can be derived. By itself, the \&\f(CW\*(C`Class::Singleton\*(C'\fR module does very little other than manage the instantiation of a single object. In deriving a class from \f(CW\*(C`Class::Singleton\*(C'\fR, your module will inherit the Singleton instantiation method and can implement whatever specific functionality is required. .PP For a description and discussion of the Singleton class, see \&\*(L"Design Patterns\*(R", Gamma et al, Addison-Wesley, 1995, \s-1ISBN 0\-201\-63361\-2.\s0 .SS "Using the Class::Singleton Module" .IX Subsection "Using the Class::Singleton Module" To import and use the \f(CW\*(C`Class::Singleton\*(C'\fR module the following line should appear in your Perl program: .PP .Vb 1 \& use Class::Singleton; .Ve .PP The \fBinstance()\fR method is used to create a new \f(CW\*(C`Class::Singleton\*(C'\fR instance, or return a reference to an existing instance. Using this method, it is only possible to have a single instance of the class in any system. .PP .Vb 1 \& my $highlander = Class::Singleton\->instance(); .Ve .PP Assuming that no \f(CW\*(C`Class::Singleton\*(C'\fR object currently exists, this first call to \fBinstance()\fR will create a new \f(CW\*(C`Class::Singleton\*(C'\fR and return a reference to it. Future invocations of \fBinstance()\fR will return the same reference. .PP .Vb 1 \& my $macleod = Class::Singleton\->instance(); .Ve .PP In the above example, both \f(CW$highlander\fR and \f(CW$macleod\fR contain the same reference to a \f(CW\*(C`Class::Singleton\*(C'\fR instance. There can be only one. .SS "Deriving Singleton Classes" .IX Subsection "Deriving Singleton Classes" A module class may be derived from \f(CW\*(C`Class::Singleton\*(C'\fR and will inherit the \&\fBinstance()\fR method that correctly instantiates only one object. .PP .Vb 2 \& package PrintSpooler; \& use base \*(AqClass::Singleton\*(Aq; \& \& # derived class specific code \& sub submit_job { \& ... \& } \& \& sub cancel_job { \& ... \& } .Ve .PP The \f(CW\*(C`PrintSpooler\*(C'\fR class defined above could be used as follows: .PP .Vb 1 \& use PrintSpooler; \& \& my $spooler = PrintSpooler\->instance(); \& \& $spooler\->submit_job(...); .Ve .PP The \fBinstance()\fR method calls the \fB_new_instance()\fR constructor method the first and only time a new instance is created. All parameters passed to the \&\fBinstance()\fR method are forwarded to \fB_new_instance()\fR. In the base class the \fB_new_instance()\fR method returns a blessed reference to a hash array containing any arguments passed as either a hash reference or list of named parameters. .PP .Vb 2 \& package MyConfig; \& use base \*(AqClass::Singleton\*(Aq; \& \& sub foo { \& shift\->{ foo }; \& } \& \& sub bar { \& shift\->{ bar }; \& } \& \& package main; \& \& # either: hash reference of named parameters \& my $config = MyConfig\->instance({ foo => 10, bar => 20 }); \& \& # or: list of named parameters \& my $config = MyConfig\->instance( foo => 10, bar => 20 ); \& \& print $config\->foo(); # 10 \& print $config\->bar(); # 20 .Ve .PP Derived classes may redefine the \fB_new_instance()\fR method to provide more specific object initialisation or change the underlying object type (to a list reference, for example). .PP .Vb 3 \& package MyApp::Database; \& use base \*(AqClass::Singleton\*(Aq; \& use DBI; \& \& # this only gets called the first time instance() is called \& sub _new_instance { \& my $class = shift; \& my $self = bless { }, $class; \& my $db = shift || "myappdb"; \& my $host = shift || "localhost"; \& \& $self\->{ DB } = DBI\->connect("DBI:mSQL:$db:$host") \& || die "Cannot connect to database: $DBI::errstr"; \& \& # any other initialisation... \& \& return $self; \& } .Ve .PP The above example might be used as follows: .PP .Vb 1 \& use MyApp::Database; \& \& # first use \- database gets initialised \& my $database = MyApp::Database\->instance(); .Ve .PP Some time later on in a module far, far away... .PP .Vb 2 \& package MyApp::FooBar \& use MyApp::Database; \& \& # this FooBar object needs access to the database; the Singleton \& # approach gives a nice wrapper around global variables. \& \& sub new { \& my $class = shift; \& bless { \& database => MyApp::Database\->instance(), \& }, $class; \& } .Ve .PP The \f(CW\*(C`Class::Singleton\*(C'\fR \fBinstance()\fR method uses a private hash to store a reference to any existing instance of the object, keyed against the derived class package name. .PP This allows different classes to be derived from \f(CW\*(C`Class::Singleton\*(C'\fR that can co-exist in the same system, while still allowing only one instance of any one class to exist. For example, it would be possible to derive both \&'\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 single instance of \fIeach\fR in a system, rather than a single instance of \&\fIeither\fR. .PP You can use the \fBhas_instance()\fR method to find out if a particular class already has an instance defined. A reference to the instance is returned or \&\f(CW\*(C`undef\*(C'\fR if none is currently defined. .PP .Vb 2 \& my $instance = MyApp::Database\->has_instance() \& || warn "No instance is defined yet"; .Ve .SS "Methods" .IX Subsection "Methods" .IP "\fBinstance()\fR" 4 .IX Item "instance()" This method is called to return a current object instance or create a new one by calling \fB_new_instance()\fR. .IP "\fBhas_instance()\fR" 4 .IX Item "has_instance()" This method returns a reference to any existing instance or \f(CW\*(C`undef\*(C'\fR if none is defined. .Sp .Vb 2 \& my $testing = MySingleton1\->has_instance() \& || warn "No instance defined for MySingleton1"; .Ve .IP "\fB_new_instance()\fR" 4 .IX Item "_new_instance()" This \*(L"private\*(R" method is called by \fBinstance()\fR to create a new object instance if one doesn't already exist. It is not intended to be called directly (although there's nothing to stop you from calling it if you're really determined to do so). .Sp It creates a blessed hash reference containing any arguments passed to the method as either a hash reference or list of named parameters. .Sp .Vb 2 \& # either: hash reference of named parameters \& my $example1 = MySingleton1\->new({ pi => 3.14, e => 2.718 }); \& \& # or: list of named parameters \& my $example2 = MySingleton2\->new( pi => 3.14, e => 2.718 ); .Ve .Sp It is important to remember that the \fBinstance()\fR method will \fIonly\fR call the \fI\f(BI_new_instance()\fI\fR method once, so any arguments you pass may be silently ignored if an instance already exists. You can use the \fBhas_instance()\fR method to determine if an instance is already defined. .SH "EXPORTS" .IX Header "EXPORTS" \&\fINone\fR. .SH "KNOWN BUGS" .IX Header "KNOWN BUGS" \&\fINone\fR. .SH "FEEDBACK" .IX Header "FEEDBACK" Patches, bug reports, suggestions or any other feedback is welcome. .PP Patches can be sent as GitHub pull requests at . .PP Bug reports and suggestions can be made on the \s-1CPAN\s0 Request Tracker at . .PP Currently active requests on the \s-1CPAN\s0 Request Tracker can be viewed at . .PP Please test this distribution. See \s-1CPAN\s0 Testers Reports at for details of how to get involved. .PP Previous test results on \s-1CPAN\s0 Testers Reports can be viewed at . .PP Please rate this distribution on \s-1CPAN\s0 Ratings at . .SH "AVAILABILITY" .IX Header "AVAILABILITY" The latest version of this module is available from \s-1CPAN\s0 (see \&\*(L"\s-1CPAN\*(R"\s0 in perlmodlib for details) at .PP or .PP or .PP . .PP The latest source code is available from GitHub at . .SH "INSTALLATION" .IX Header "INSTALLATION" See the \fI\s-1INSTALL\s0\fR file. .SH "AUTHOR" .IX Header "AUTHOR" Andy Wardley > . .PP Thanks to Andreas Koenig for providing some significant speedup patches and other ideas. .PP Steve Hay > is now maintaining Class::Singleton as of version 1.5. .SH "COPYRIGHT" .IX Header "COPYRIGHT" Copyright (C) 1998 Canon Research Centre Europe Ltd. .PP Copyright (C) 1998\-2008 Andy Wardley. All rights reserved. .PP Copyright (C) 2014, 2020 Steve Hay. All rights reserved. .SH "LICENCE" .IX Header "LICENCE" This module is free software; you can redistribute it and/or modify it under the same terms as Perl itself, i.e. under the terms of either the \s-1GNU\s0 General Public License or the Artistic License, as specified in the \fI\s-1LICENCE\s0\fR file. .SH "VERSION" .IX Header "VERSION" Version 1.6 .SH "DATE" .IX Header "DATE" 02 Dec 2020 .SH "HISTORY" .IX Header "HISTORY" See the \fIChanges\fR file.