| 1 | package XAS::Singleton; | ||
| 2 | |||
| 3 | our $VERSION = '0.01'; | ||
| 4 | |||
| 5 | use XAS::Class | ||
| 6 | debug => 0, | ||
| 7 | version => $VERSION, | ||
| 8 | base => 'XAS::Base', | ||
| 9 | constants => 'HASH', | ||
| 10 | ; | ||
| 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 | my $class = shift; | ||
| 23 | |||
| 24 | # already got an object | ||
| 25 | |||
| 26 | return $class if ref $class; | ||
| 27 | |||
| 28 | # we store the instance in the _instance variable in the $class package. | ||
| 29 | |||
| 30 | no strict 'refs'; | ||
| 31 | my $instance = \${ "$class\::_instance" }; | ||
| 32 | |||
| 33 | defined $$instance | ||
| 34 | ? $$instance | ||
| 35 | : ($$instance = $class->_new_instance(@_)); | ||
| 36 | |||
| 37 | } | ||
| 38 | |||
| 39 | # ---------------------------------------------------------------------- | ||
| 40 | # Private Methods | ||
| 41 | # ---------------------------------------------------------------------- | ||
| 42 | |||
| 43 | sub _new_instance { | ||
| 44 | my $class = shift; | ||
| 45 | |||
| 46 | # install warning handling for odd number of parameters when DEBUG enabled | ||
| 47 | |||
| 48 | local $SIG{__WARN__} = sub { | ||
| 49 | Badger::Utils::odd_params(@_); | ||
| 50 | } if DEBUG; | ||
| 51 | |||
| 52 | my $args = @_ && ref $_[0] eq HASH ? shift : { @_ }; | ||
| 53 | my $self = bless { }, ref $class || $class; | ||
| 54 | |||
| 55 | $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 | 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 library is free software; you can redistribute it and/or modify | ||
| 108 | it under the same terms as Perl itself, either Perl version 5.8.8 or, | ||
| 109 | at your option, any later version of Perl 5 you may have available. | ||
| 110 | |||
| 111 | See L<http://dev.perl.org/licenses/> for more information. | ||
| 112 | |||
| 113 | =cut |