File Coverage

blib/lib/Apache/Singleton.pm
Criterion Covered Total %
statement 19 22 86.3
branch 5 8 62.5
condition 2 3 66.6
subroutine 6 6 100.0
pod 0 1 0.0
total 32 40 80.0


line stmt bran cond sub pod time code
1             package Apache::Singleton;
2             BEGIN {
3 7     7   124211 $Apache::Singleton::VERSION = '0.15';
4             }
5              
6             # ABSTRACT: Singleton class for mod_perl
7              
8 7     7   134 use strict;
  7         13  
  7         2761  
9              
10             # load appropriate subclass
11             if ($ENV{MOD_PERL}) {
12             require Apache::Singleton::Request;
13             }
14             else {
15             require Apache::Singleton::Process;
16             }
17              
18             sub instance {
19 22     22 0 859 my $class = shift;
20              
21 22         110 my $instance = $class->_get_instance;
22 22 100       116 unless (defined $instance) {
23 10         82 $instance = $class->_new_instance(@_);
24 10         80 $class->_set_instance($instance);
25             }
26 22         704 return $instance;
27             }
28              
29             sub _new_instance {
30 10     10   20 my $class = shift;
31              
32 10 50 66     73 my %args = (@_ && ref $_[0] eq 'HASH') ? %{ $_[0] } : @_;
  0         0  
33              
34 10         50 bless { %args }, $class;
35             }
36              
37             # Abstract methods, but compatible default
38             sub _get_instance {
39 10     10   16 my $class = shift;
40              
41 10 50       33 if ($ENV{MOD_PERL}) {
42 0         0 $class->Apache::Singleton::Request::_get_instance(@_);
43             }
44             else {
45 10         58 $class->Apache::Singleton::Process::_get_instance(@_);
46             }
47             }
48              
49             sub _set_instance {
50 4     4   8 my $class = shift;
51              
52 4 50       15 if ($ENV{MOD_PERL}) {
53 0         0 $class->Apache::Singleton::Request::_set_instance(@_);
54             }
55             else {
56 4         24 $class->Apache::Singleton::Process::_set_instance(@_);
57             }
58             }
59              
60             1;
61              
62              
63             =pod
64              
65             =head1 NAME
66              
67             Apache::Singleton - Singleton class for mod_perl
68              
69             =head1 VERSION
70              
71             version 0.15
72              
73             =head1 SYNOPSIS
74              
75             package Printer;
76             # default:
77             # Request for mod_perl env
78             # Process for non-mod_perl env
79             use base qw(Apache::Singleton);
80              
81             package Printer::PerRequest;
82             use base qw(Apache::Singleton::Request);
83              
84             package Printer::PerProcess;
85             use base qw(Apache::Singleton::Process);
86              
87             =head1 DESCRIPTION
88              
89             Apache::Singleton works the same as Class::Singleton, but with
90             various object lifetime (B). See L first.
91              
92             =head1 OBJECT LIFETIME
93              
94             By inheriting one of the following sublasses of Apache::Singleton,
95             you can change the scope of your object.
96              
97             =over 4
98              
99             =item Request
100              
101             use base qw(Apache::Singleton::Request);
102              
103             One instance for one request. Apache::Singleton will remove instance
104             on each request. Implemented using mod_perl C API. In mod_perl
105             environment (where C<$ENV{MOD_PERL}> is defined), this is the default
106             scope, so inheriting from Apache::Singleton would do the same effect.
107              
108             B: You need C in your apache
109             configuration in order to use the I lifetime method.
110              
111             =item Process
112              
113             use base qw(Apache::Singleton::Process);
114              
115             One instance for one httpd process. Implemented using package
116             global. In non-mod_perl environment, this is the default scope, and
117             you may notice this is the same beaviour with Class::Singleton ;)
118              
119             So you can use this module safely under non-mod_perl environment.
120              
121             =back
122              
123             =head1 CREDITS
124              
125             Original idea by Matt Sergeant Ematt@sergeant.orgE and Perrin
126             Harkins Eperrin@elem.comE.
127              
128             Initial implementation and versions 0.01 to 0.07 by Tatsuhiko Miyagawa
129             Emiyagawa@bulknews.netE.
130              
131             =head1 SEE ALSO
132              
133             L, L,
134             L
135              
136             =head1 SOURCE
137              
138             The development version is on github at L
139             and may be cloned from L
140              
141             =head1 BUGS
142              
143             Please report any bugs or feature requests to bug-apache-singleton@rt.cpan.org or through the web interface at:
144             http://rt.cpan.org/Public/Dist/Display.html?Name=Apache-Singleton
145              
146             =head1 AUTHOR
147              
148             Michael Schout
149              
150             =head1 COPYRIGHT AND LICENSE
151              
152             This software is copyright (c) 2009 by Michael Schout.
153              
154             This is free software; you can redistribute it and/or modify it under
155             the same terms as the Perl 5 programming language system itself.
156              
157             =cut
158              
159              
160             __END__