| 1 | package Module::Pluggable::Singleton; | ||||||||||||
| 2 | { | ||||||||||||
| 3 | 2 | $Module::Pluggable::Singleton::VERSION = '0.06'; | |||||||||||
| 4 | } | ||||||||||||
| 5 | { | ||||||||||||
| 6 | 2 | $Module::Pluggable::Singleton::DIST = 'Module-Pluggable-Singleton'; | |||||||||||
| 7 | } | ||||||||||||
| 8 | |||||||||||||
| 9 | 2 | ▶ | use strict; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 10 | 2 | ▶ | use warnings; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 11 | 2 | ▶ | use Module::Pluggable::Singleton::Object; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 12 | 2 | ▶ | use Carp; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 13 | 2 | ▶ | use Data::Dump qw/pp/; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 14 | |||||||||||||
| 15 | =head1 NAME | ||||||||||||
| 16 | |||||||||||||
| 17 | Module::Pluggable::Singleton - call/return single plugins on demand using shortened names | ||||||||||||
| 18 | |||||||||||||
| 19 | =head1 VERSION | ||||||||||||
| 20 | |||||||||||||
| 21 | version 0.06 | ||||||||||||
| 22 | |||||||||||||
| 23 | =cut | ||||||||||||
| 24 | |||||||||||||
| 25 | =head1 SYNOPSIS | ||||||||||||
| 26 | |||||||||||||
| 27 | package MyPluggable; | ||||||||||||
| 28 | use Module::Pluggable::Singleton; | ||||||||||||
| 29 | |||||||||||||
| 30 | my $plugins = MyPluggable->plugin; | ||||||||||||
| 31 | my @plugins = MyPluggable->plugin; | ||||||||||||
| 32 | |||||||||||||
| 33 | my @plugins = MyPluggable->plugins; | ||||||||||||
| 34 | |||||||||||||
| 35 | my $plugin_long = $logic->plugin($name); | ||||||||||||
| 36 | |||||||||||||
| 37 | my $plugins = MyPluggable->plugin; | ||||||||||||
| 38 | my $nick = MyPluggable->call('Robert','nickname',$person->full_name); | ||||||||||||
| 39 | |||||||||||||
| 40 | =head1 DESCRIPTION | ||||||||||||
| 41 | |||||||||||||
| 42 | =head1 METHODS | ||||||||||||
| 43 | |||||||||||||
| 44 | =head2 find($name) | ||||||||||||
| 45 | |||||||||||||
| 46 | Return the instance of the plugin with given short name | ||||||||||||
| 47 | |||||||||||||
| 48 | $plugin = $logic->find('Bar'); | ||||||||||||
| 49 | |||||||||||||
| 50 | =head2 plugin($name) | ||||||||||||
| 51 | |||||||||||||
| 52 | Return an array of the shortened available plugins or the full name of the | ||||||||||||
| 53 | module when a short name is given | ||||||||||||
| 54 | |||||||||||||
| 55 | my @plugin_short = $logic->plugin | ||||||||||||
| 56 | my $plugin_long = $logic->plugin($name); | ||||||||||||
| 57 | |||||||||||||
| 58 | =head2 plugin($name) | ||||||||||||
| 59 | |||||||||||||
| 60 | Return an array of all the module names | ||||||||||||
| 61 | |||||||||||||
| 62 | =head2 call($plugin_name, $method, $params) | ||||||||||||
| 63 | |||||||||||||
| 64 | Using the plugin $plugin_name call $method with $params | ||||||||||||
| 65 | |||||||||||||
| 66 | $rv = $logic->call($plugin_name, $method, $params) | ||||||||||||
| 67 | |||||||||||||
| 68 | =cut | ||||||||||||
| 69 | |||||||||||||
| 70 | 1 | ▶ | sub import { | ||||||||||
|
Subroutine
import: called CC=6 SCAR=18.9
| |||||||||||||
| 71 | 1 | my ($class, %opts) = @_; | |||||||||||
| 72 | 1 | my $caller = (caller)[0]; | |||||||||||
| 73 | 1 | $opts{require} = 1; # you find out earlier if it has a syntax error | |||||||||||
| 74 | 1 | $opts{package} = $caller; | |||||||||||
| 75 | |||||||||||||
| 76 | 1 | my $finder = Module::Pluggable::Singleton::Object->new(%opts); | |||||||||||
| 77 | |||||||||||||
| 78 | 1 | ▶ | if (!$opts{search_path}) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 79 | 1 | $opts{search_path} = "${caller}::Plugin"; | |||||||||||
| 80 | } | ||||||||||||
| 81 | 1 | ▶ | if ($opts{search_path}) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 82 | 1 | ▶ | if (ref($opts{search_path}) eq '') { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 83 | 1 | $opts{search_path} = [$opts{search_path}]; | |||||||||||
| 84 | } | ||||||||||||
| 85 | } | ||||||||||||
| 86 | |||||||||||||
| 87 | 1 | ▶ | my $namespace = "${caller}::". ucfirst($opts{sub_name} || 'plugins'); | ||||||||||
|
Condition50%1/2
$opts{'sub_name'} || 'plugins'
MC/DC0%0/1
$opts{'sub_name'} || 'plugins'
$opts{'sub_name'}
Truth table50%1/2
A
A= $opts{'sub_name'}
| |||||||||||||
| 88 | 1 | ▶ | my $sub_name = $opts{sub_name} || 'plugins'; | ||||||||||
|
Condition50%1/2
$opts{'sub_name'} || 'plugins'
MC/DC0%0/1
$opts{'sub_name'} || 'plugins'
$opts{'sub_name'}
Truth table50%1/2
A
A= $opts{'sub_name'}
| |||||||||||||
| 89 | 1 | my $plugin_for = { }; # maps shortname to module name | |||||||||||
| 90 | 1 | my $instance_of = { }; # instances | |||||||||||
| 91 | |||||||||||||
| 92 | 0 | ▶ | my $find_sub = sub { | ||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||
| 93 | 0 | shift @_; | |||||||||||
| 94 | 0 | return $finder->find(@_); | |||||||||||
| 95 | 1 | }; | |||||||||||
| 96 | |||||||||||||
| 97 | 0 | ▶ | my $plugin_sub = sub { | ||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||
| 98 | 0 | shift @_; | |||||||||||
| 99 | 0 | return $finder->plugin(@_); | |||||||||||
| 100 | 1 | }; | |||||||||||
| 101 | 0 | ▶ | my $call_sub = sub { | ||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||
| 102 | 0 | shift @_; | |||||||||||
| 103 | 0 | return $finder->call(@_); | |||||||||||
| 104 | 1 | }; | |||||||||||
| 105 | |||||||||||||
| 106 | 0 | ▶ | my $plugins_sub = sub { | ||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||
| 107 | 0 | my($self) = @_; | |||||||||||
| 108 | 0 | $finder->plugins(@_); | |||||||||||
| 109 | 1 | }; | |||||||||||
| 110 | 2 | ▶ | no strict 'refs'; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 111 | 2 | ▶ | no warnings qw(redefine prototype); | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 112 | |||||||||||||
| 113 | 1 | *{"$caller\::$sub_name"} = $plugins_sub; | |||||||||||
| 114 | 1 | *{"$caller\::find"} = $find_sub; | |||||||||||
| 115 | 1 | *{"$caller\::plugin"} = $plugin_sub; | |||||||||||
| 116 | 1 | *{"$caller\::call"} = $call_sub; | |||||||||||
| 117 | |||||||||||||
| 118 | 1 | return; | |||||||||||
| 119 | } | ||||||||||||
| 120 | |||||||||||||
| 121 | =head1 AUTHOR | ||||||||||||
| 122 | |||||||||||||
| 123 | Jason Tang, C<< <tang.jason.ch at gmail.com> >> | ||||||||||||
| 124 | |||||||||||||
| 125 | =head1 BUGS | ||||||||||||
| 126 | |||||||||||||
| 127 | Please report any bugs or feature requests to C<bug-module-pluggable-singleton at rt.cpan.org>, or through | ||||||||||||
| 128 | the web interface at L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Module-Pluggable-Singleton>. I will be notified, and then you'll | ||||||||||||
| 129 | automatically be notified of progress on your bug as I make changes. | ||||||||||||
| 130 | |||||||||||||
| 131 | =head1 SEE ALSO | ||||||||||||
| 132 | |||||||||||||
| 133 | Module::Pluggable::Object | ||||||||||||
| 134 | |||||||||||||
| 135 | =head1 SUPPORT | ||||||||||||
| 136 | |||||||||||||
| 137 | You can find documentation for this module with the perldoc command. | ||||||||||||
| 138 | |||||||||||||
| 139 | perldoc Module::Pluggable::Singleton | ||||||||||||
| 140 | |||||||||||||
| 141 | |||||||||||||
| 142 | You can also look for information at: | ||||||||||||
| 143 | |||||||||||||
| 144 | =over 4 | ||||||||||||
| 145 | |||||||||||||
| 146 | =item * RT: CPAN's request tracker | ||||||||||||
| 147 | |||||||||||||
| 148 | L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=Module-Pluggable-Singleton> | ||||||||||||
| 149 | |||||||||||||
| 150 | =item * AnnoCPAN: Annotated CPAN documentation | ||||||||||||
| 151 | |||||||||||||
| 152 | L<http://annocpan.org/dist/Module-Pluggable-Singleton> | ||||||||||||
| 153 | |||||||||||||
| 154 | =item * CPAN Ratings | ||||||||||||
| 155 | |||||||||||||
| 156 | L<http://cpanratings.perl.org/d/Module-Pluggable-Singleton> | ||||||||||||
| 157 | |||||||||||||
| 158 | =item * Search CPAN | ||||||||||||
| 159 | |||||||||||||
| 160 | L<http://search.cpan.org/dist/Module-Pluggable-Singleton/> | ||||||||||||
| 161 | |||||||||||||
| 162 | =back | ||||||||||||
| 163 | |||||||||||||
| 164 | |||||||||||||
| 165 | =head1 LICENSE AND COPYRIGHT | ||||||||||||
| 166 | |||||||||||||
| 167 | Copyright 2011 Jason Tang. | ||||||||||||
| 168 | |||||||||||||
| 169 | This program is free software; you can redistribute it and/or modify it | ||||||||||||
| 170 | under the terms of either: the GNU General Public License as published | ||||||||||||
| 171 | by the Free Software Foundation; or the Artistic License. | ||||||||||||
| 172 | |||||||||||||
| 173 | See http://dev.perl.org/licenses/ for more information. | ||||||||||||
| 174 | |||||||||||||
| 175 | |||||||||||||
| 176 | =cut | ||||||||||||
| 177 | |||||||||||||
| 178 | 2 | 1; # End of Module::Pluggable::Singleton | |||||||||||