File Coverage

blib/lib/XAS/Singleton.pm
Criterion Covered Total %
statement 6 18 33.3
branch 0 8 0.0
condition 0 6 0.0
subroutine 2 5 40.0
pod 1 1 100.0
total 9 38 23.6


line stmt bran cond sub pod time code
1             package XAS::Singleton;
2              
3             our $VERSION = '0.01';
4              
5             use XAS::Class
6 1         5 debug => 0,
7             version => $VERSION,
8             base => 'XAS::Base',
9             constants => 'HASH',
10 1     1   3612 ;
  1         1  
11              
12             # ----------------------------------------------------------------------
13             # Here be the best of cut-and-paste programming. This combines
14             # the guts of Class::Singleton with Badger::Base to make singletons!
15             # ----------------------------------------------------------------------
16              
17             # ----------------------------------------------------------------------
18             # Public Methods
19             # ----------------------------------------------------------------------
20              
21             sub new {
22 0     0 1   my $class = shift;
23              
24             # already got an object
25              
26 0 0         return $class if ref $class;
27              
28             # we store the instance in the _instance variable in the $class package.
29              
30 1     1   405 no strict 'refs';
  1         1  
  1         152  
31 0           my $instance = \${ "$class\::_instance" };
  0            
32              
33 0 0         defined $$instance
34             ? $$instance
35             : ($$instance = $class->_new_instance(@_));
36              
37             }
38              
39             # ----------------------------------------------------------------------
40             # Private Methods
41             # ----------------------------------------------------------------------
42              
43             sub _new_instance {
44 0     0     my $class = shift;
45              
46             # install warning handling for odd number of parameters when DEBUG enabled
47              
48             local $SIG{__WARN__} = sub {
49 0     0     Badger::Utils::odd_params(@_);
50 0           } if DEBUG;
51              
52 0 0 0       my $args = @_ && ref $_[0] eq HASH ? shift : { @_ };
53 0   0       my $self = bless { }, ref $class || $class;
54              
55 0           $self = $self->init($args);
56              
57             # be careful to account for object that overload the boolean comparison
58             # operator and may return false to a simple truth test.
59              
60 0 0         return defined $self
61             ? $self
62             : $self->error("init() method failed\n");
63              
64             }
65              
66             1;
67              
68             __END__
69              
70             =head1 NAME
71              
72             XAS::Singleton - A singleton class for the XAS environment
73              
74             =head1 SYNOPSIS
75              
76             use XAS::Class
77             version => '0.01'
78             base => 'XAS::Singleton'
79             ;
80              
81             =head1 DESCRIPTION
82              
83             There can only be one... A singleton class for the XAS environment.
84              
85             =head1 METHODS
86              
87             =head2 new
88              
89             Initalize the class.
90              
91             =head1 SEE ALSO
92              
93             =over 4
94              
95             =item L<XAS|XAS>
96              
97             =back
98              
99             =head1 AUTHOR
100              
101             Kevin L. Esteb, E<lt>kevin@kesteb.usE<gt>
102              
103             =head1 COPYRIGHT AND LICENSE
104              
105             Copyright (C) 2014 Kevin L. Esteb
106              
107             This is free software; you can redistribute it and/or modify it under
108             the terms of the Artistic License 2.0. For details, see the full text
109             of the license at http://www.perlfoundation.org/artistic_license_2_0.
110              
111             =cut