| 1 | package Module::Pluggable::Singleton::Object; | ||||||||||||||||||||||||||||
| 2 | { | ||||||||||||||||||||||||||||
| 3 | 2 | $Module::Pluggable::Singleton::Object::VERSION = '0.06'; | |||||||||||||||||||||||||||
| 4 | } | ||||||||||||||||||||||||||||
| 5 | { | ||||||||||||||||||||||||||||
| 6 | 2 | $Module::Pluggable::Singleton::Object::DIST = 'Module-Pluggable-Singleton'; | |||||||||||||||||||||||||||
| 7 | } | ||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||
| 9 | 2 | ▶ | use parent 'Module::Pluggable::Object'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | 2 | ▶ | use Carp qw/croak confess/; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 11 | 2 | ▶ | use Data::Dump q/pp/; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||
| 13 | =head1 NAME | ||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||
| 15 | Module::Pluggable::Singleton::Object | ||||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||
| 17 | =head1 VERSION | ||||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||
| 19 | version 0.06 | ||||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||
| 21 | =head1 METHODS | ||||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||
| 23 | =head2 new | ||||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||
| 25 | =cut | ||||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||
| 27 | 1 | ▶ | sub new { | ||||||||||||||||||||||||||
|
Subroutine
new: called CC=2 SCAR=7.1
Pod
documented
| |||||||||||||||||||||||||||||
| 28 | 1 | my $class = shift; | |||||||||||||||||||||||||||
| 29 | 1 | my %opts = @_; | |||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||
| 31 | 1 | $opts{require} = 1; | |||||||||||||||||||||||||||
| 32 | 1 | my $obj = Module::Pluggable::Object->new(%opts); | |||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||
| 34 | 1 | my $self = bless $obj, $class; | |||||||||||||||||||||||||||
| 35 | 1 | ▶ | $self->_parse_plugins($opts{package} || $class); | ||||||||||||||||||||||||||
|
Condition33%1/3
$opts{'package'} || $class
MC/DC0%0/2
$opts{'package'} || $class
$opts{'package'}
$class
Truth table33%1/3
A || B
A= $opts{'package'}B= $class
| |||||||||||||||||||||||||||||
| 36 | |||||||||||||||||||||||||||||
| 37 | 1 | return $self; | |||||||||||||||||||||||||||
| 38 | } | ||||||||||||||||||||||||||||
| 39 | |||||||||||||||||||||||||||||
| 40 | |||||||||||||||||||||||||||||
| 41 | 1 | ▶ | sub _parse_plugins { | ||||||||||||||||||||||||||
|
Subroutine
_parse_plugins: called CC=10 SCAR=29.2
| |||||||||||||||||||||||||||||
| 42 | 1 | my($self,$caller) = @_; | |||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||
| 44 | 1 | ▶ | if (!$self->{search_path}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 45 | 1 | $self->{search_path} = "${caller}::Plugin"; | |||||||||||||||||||||||||||
| 46 | } | ||||||||||||||||||||||||||||
| 47 | 1 | ▶ | if ($self->{search_path}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 48 | 1 | ▶ | if (ref($self->{search_path}) eq '') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 49 | 1 | $self->{search_path} = [$self->{search_path}]; | |||||||||||||||||||||||||||
| 50 | } | ||||||||||||||||||||||||||||
| 51 | } | ||||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||
| 53 | warn "SEARCH_PATH: ". pp($self->{search_path}) | ||||||||||||||||||||||||||||
| 54 | 1 | ▶ | if ($self->{debug}); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||
| 57 | 1 | ▶ | my $namespace = "${caller}::". ucfirst($self->{sub_name} || 'plugins'); | ||||||||||||||||||||||||||
|
Condition50%1/2
$self->{'sub_name'} || 'plugins'
MC/DC0%0/1
$self->{'sub_name'} || 'plugins'
$self->{'sub_name'}
Truth table50%1/2
A
A= $self->{'sub_name'}
| |||||||||||||||||||||||||||||
| 58 | 1 | ▶ | my $sub_name = $self->{sub_name} || 'plugins'; | ||||||||||||||||||||||||||
|
Condition50%1/2
$self->{'sub_name'} || 'plugins'
MC/DC0%0/1
$self->{'sub_name'} || 'plugins'
$self->{'sub_name'}
Truth table50%1/2
A
A= $self->{'sub_name'}
| |||||||||||||||||||||||||||||
| 59 | 1 | my $plugin_for = { }; # maps shortname to module name | |||||||||||||||||||||||||||
| 60 | 1 | my $instance_of = { }; # instances | |||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||
| 62 | |||||||||||||||||||||||||||||
| 63 | 1 | foreach my $plugin ($self->plugins) { | |||||||||||||||||||||||||||
| 64 | 0 | my $shortname = $plugin; | |||||||||||||||||||||||||||
| 65 | |||||||||||||||||||||||||||||
| 66 | 0 | foreach my $path (@{$self->{search_path}}) { | |||||||||||||||||||||||||||
| 67 | 0 | $shortname =~ s/^${path}:://; | |||||||||||||||||||||||||||
| 68 | } | ||||||||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||||||
| 70 | # FIXME: | ||||||||||||||||||||||||||||
| 71 | # if (not $plugin->isa($base_class)) { | ||||||||||||||||||||||||||||
| 72 | # confess __PACKAGE__ .": plugin '$shortname' needs to implement " | ||||||||||||||||||||||||||||
| 73 | # ."'". $base_class ."'"; | ||||||||||||||||||||||||||||
| 74 | # } | ||||||||||||||||||||||||||||
| 75 | |||||||||||||||||||||||||||||
| 76 | 0 | ▶ | if (exists $plugin_for->{$shortname}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 77 | 0 | confess "$caller: Plugin already exists for '$shortname'"; | |||||||||||||||||||||||||||
| 78 | } | ||||||||||||||||||||||||||||
| 79 | |||||||||||||||||||||||||||||
| 80 | 0 | $plugin_for->{$shortname} = $plugin; | |||||||||||||||||||||||||||
| 81 | } | ||||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||
| 83 | 1 | $self->{plugin_for} = $plugin_for; | |||||||||||||||||||||||||||
| 84 | } | ||||||||||||||||||||||||||||
| 85 | |||||||||||||||||||||||||||||
| 86 | =head2 find | ||||||||||||||||||||||||||||
| 87 | |||||||||||||||||||||||||||||
| 88 | =cut | ||||||||||||||||||||||||||||
| 89 | |||||||||||||||||||||||||||||
| 90 | 0 | ▶ | sub find { | ||||||||||||||||||||||||||
|
Subroutine
find: not called CC=10 SCAR=47.0
Pod
documented
| |||||||||||||||||||||||||||||
| 91 | 0 | my($self,$shortname) = @_; | |||||||||||||||||||||||||||
| 92 | |||||||||||||||||||||||||||||
| 93 | 0 | ▶ | if (!defined $shortname) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 94 | 0 | die "Not provided name of plugin"; | |||||||||||||||||||||||||||
| 95 | 0 | return; | |||||||||||||||||||||||||||
| 96 | } | ||||||||||||||||||||||||||||
| 97 | |||||||||||||||||||||||||||||
| 98 | 0 | ▶ | my $name = $self->{plugin_for}->{$shortname} || undef; | ||||||||||||||||||||||||||
|
Condition0%0/2
$self->{'plugin_for'}{$shortname} || undef
MC/DC0%0/1
$self->{'plugin_for'}{$shortname} || undef
$self->{'plugin_for'}{$shortname}
Truth table0%0/2
A
A= $self->{'plugin_for'}{$shortname}
| |||||||||||||||||||||||||||||
| 99 | 0 | ▶ | if (!defined $name) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 100 | 0 | die "Not possible to load module '$shortname'"; | |||||||||||||||||||||||||||
| 101 | } | ||||||||||||||||||||||||||||
| 102 | |||||||||||||||||||||||||||||
| 103 | |||||||||||||||||||||||||||||
| 104 | # use an existing instance or create a new one.. and keep ref to it | ||||||||||||||||||||||||||||
| 105 | 0 | ▶ | my $instance_of = $self->{instance_of} // { }; | ||||||||||||||||||||||||||
|
Condition0%0/2
$self->{"instance_of"} // {}
MC/DC0%0/1
$self->{"instance_of"} // {}
$self->{"instance_of"}
Truth table0%0/2
A
A= $self->{"instance_of"}
| |||||||||||||||||||||||||||||
| 106 | 0 | ▶ | my $instance = $instance_of->{$shortname} | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$instance_of->{$shortname} || (defined $name ? $name->new : undef)
MC/DC0%0/2
$instance_of->{$shortname} || (defined $name ? $name->new : undef)
$instance_of->{$shortname}
(defined $name ? $name->new : undef)
Truth table0%0/3
A || B
A= $instance_of->{$shortname}B= (defined $name ? $name->new : undef)
| |||||||||||||||||||||||||||||
| 107 | || (defined $name ? $name->new() : undef); | ||||||||||||||||||||||||||||
| 108 | |||||||||||||||||||||||||||||
| 109 | 0 | ▶ | if ($instance && not defined $instance_of->{$shortname}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$instance and not defined $instance_of->{$shortname}
MC/DC0%0/2
$instance and not defined $instance_of->{$shortname}
$instance
not defined $instance_of->{$shortname}
Truth table0%0/3
A and not(B)
A= $instanceB= not defined $instance_of->{$shortname}
| |||||||||||||||||||||||||||||
| 110 | 0 | $instance_of->{$shortname} = $instance; | |||||||||||||||||||||||||||
| 111 | } | ||||||||||||||||||||||||||||
| 112 | |||||||||||||||||||||||||||||
| 113 | # if we didn't already the hash ref created | ||||||||||||||||||||||||||||
| 114 | 0 | ▶ | $self->{instance_of} = $instance_of if (!defined $self->{instance_of}); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 115 | |||||||||||||||||||||||||||||
| 116 | 0 | return $instance; | |||||||||||||||||||||||||||
| 117 | } | ||||||||||||||||||||||||||||
| 118 | |||||||||||||||||||||||||||||
| 119 | =head2 plugin | ||||||||||||||||||||||||||||
| 120 | |||||||||||||||||||||||||||||
| 121 | =cut | ||||||||||||||||||||||||||||
| 122 | |||||||||||||||||||||||||||||
| 123 | 0 | ▶ | sub plugin { | ||||||||||||||||||||||||||
|
Subroutine
plugin: not called CC=3 SCAR=24.8
Pod
documented
| |||||||||||||||||||||||||||||
| 124 | 0 | my($self,$shortname) = @_; | |||||||||||||||||||||||||||
| 125 | 0 | my $plugin_for = $self->{plugin_for}; | |||||||||||||||||||||||||||
| 126 | |||||||||||||||||||||||||||||
| 127 | 0 | ▶ | return keys %{$plugin_for} if (!defined $shortname); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 128 | |||||||||||||||||||||||||||||
| 129 | return defined $plugin_for->{$shortname} | ||||||||||||||||||||||||||||
| 130 | 0 | ▶ | ? $plugin_for->{$shortname} : undef; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 131 | |||||||||||||||||||||||||||||
| 132 | } | ||||||||||||||||||||||||||||
| 133 | |||||||||||||||||||||||||||||
| 134 | =head2 call | ||||||||||||||||||||||||||||
| 135 | |||||||||||||||||||||||||||||
| 136 | =cut | ||||||||||||||||||||||||||||
| 137 | |||||||||||||||||||||||||||||
| 138 | 0 | ▶ | sub call { | ||||||||||||||||||||||||||
|
Subroutine
call: not called CC=4 SCAR=30.0
Pod
documented
| |||||||||||||||||||||||||||||
| 139 | 0 | my($self,$shortname,$method) = @_; | |||||||||||||||||||||||||||
| 140 | |||||||||||||||||||||||||||||
| 141 | 0 | ▶ | if (!defined $shortname) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 142 | 0 | die "$caller: Plugin name not provided"; | |||||||||||||||||||||||||||
| 143 | 0 | return; | |||||||||||||||||||||||||||
| 144 | } | ||||||||||||||||||||||||||||
| 145 | |||||||||||||||||||||||||||||
| 146 | 0 | ▶ | if (!defined $method) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 147 | 0 | die "Method name not provided"; | |||||||||||||||||||||||||||
| 148 | 0 | return; | |||||||||||||||||||||||||||
| 149 | } | ||||||||||||||||||||||||||||
| 150 | |||||||||||||||||||||||||||||
| 151 | |||||||||||||||||||||||||||||
| 152 | 0 | my $instance = $self->find($shortname); | |||||||||||||||||||||||||||
| 153 | 0 | ▶ | if (!$instance->can($method)) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 154 | 0 | die "Cannot call '$method' on '$shortname' plugin"; | |||||||||||||||||||||||||||
| 155 | } | ||||||||||||||||||||||||||||
| 156 | |||||||||||||||||||||||||||||
| 157 | 0 | return $instance->$method(@_); | |||||||||||||||||||||||||||
| 158 | |||||||||||||||||||||||||||||
| 159 | } | ||||||||||||||||||||||||||||
| 160 | |||||||||||||||||||||||||||||
| 161 | 2 | 1; | |||||||||||||||||||||||||||