| 1 | # | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2 | # Sub::Contract::Pool - The pool of contracts | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3 | # | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4 | # $Id: Pool.pm,v 1.15 2009/06/16 12:23:58 erwan_lemonnier Exp $ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 5 | # | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 7 | package Sub::Contract::Pool; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 9 | 23 | ▶ | use strict; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 10 | 23 | ▶ | use warnings; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 12 | 23 | ▶ | use Carp qw(croak); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 14 | 23 | ▶ | use vars qw($AUTOLOAD); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 15 | 23 | ▶ | use accessors qw( _contract_index | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 16 | 23 | ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 18 | 23 | ▶ | use base qw(Exporter); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 20 | 23 | our $VERSION = '0.12'; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 22 | 23 | our @EXPORT = (); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 23 | 23 | our @EXPORT_OK = ('get_contract_pool'); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 25 | #--------------------------------------------------------------- | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 26 | # | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 27 | # A singleton pattern with lazy initialization and embedded constructor | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 28 | # | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 30 | 23 | my $pool; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 32 | 25 | ▶ | sub get_contract_pool { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
get_contract_pool: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 33 | 25 | ▶ | if (!defined $pool) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 34 | 23 | $pool = bless({},__PACKAGE__); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 35 | 23 | $pool->_contract_index({}); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 36 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 37 | 25 | return $pool; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 38 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 39 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 40 | #--------------------------------------------------------------- | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 41 | # | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 42 | # list_all_contracts - return all contracts registered in the pool | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 43 | # | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 45 | 21 | ▶ | sub list_all_contracts { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
list_all_contracts: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 46 | 21 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 47 | 21 | return values %{$self->_contract_index}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 48 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 50 | #--------------------------------------------------------------- | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 51 | # | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 52 | # has_contract - | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 53 | # | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 54 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 55 | # TODO: should it be removed? to use find_contract instead? would it be too slow? | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 57 | 151 | ▶ | sub has_contract { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
has_contract: called CC=5 SCAR=16.2
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 58 | 151 | my ($self, $contractor) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 60 | 151 | ▶ | croak "method has_contract() expects a fully qualified function name as argument" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition77%7/9
scalar @_ != 2 or not defined $contractor or ref $contractor ne "" or not $contractor =~ /::/
scalar @_ != 2 or not defined $contractor or ref $contractor ne ""
scalar @_ != 2 or not defined $contractor
MC/DC50%2/4
scalar @_ != 2 or not defined $contractor or ref $contractor ne "" or not $contractor =~ /::/
scalar @_ != 2
not defined $contractor
ref $contractor ne ""
not $contractor =~ /::/
Truth table60%3/5
A or not(B) or C or not(D)
A= scalar @_ != 2B= not defined $contractorC= ref $contractor ne ""D= not $contractor =~ /::/
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 61 | if ( scalar @_ != 2 || | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 62 | !defined $contractor || | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 63 | ref $contractor ne '' || | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 64 | $contractor !~ /::/ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 65 | ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 66 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 67 | 144 | my $index = $self->_contract_index; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 68 | 144 | return exists $index->{$contractor}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 69 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 71 | #--------------------------------------------------------------- | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 72 | # | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 73 | # _add_contract | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 74 | # | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 75 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 76 | 49 | ▶ | sub _add_contract { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_add_contract: called CC=5 SCAR=17.3
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 77 | 49 | my ($self, $contract) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 78 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 79 | 49 | ▶ | croak "method add_contract() expects only 1 argument" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 80 | if (scalar @_ != 2); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 81 | 49 | ▶ | croak "method add_contract() expects an instance of Sub::contract as argument" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
not defined $contract or ref $contract ne "Sub::Contract"
MC/DC0%0/2
not defined $contract or ref $contract ne "Sub::Contract"
not defined $contract
ref $contract ne "Sub::Contract"
Truth table33%1/3
not(A) or B
A= not defined $contractB= ref $contract ne "Sub::Contract"
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 82 | if (!defined $contract || ref $contract ne 'Sub::Contract'); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 83 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 84 | 49 | my $index = $self->_contract_index; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 85 | 49 | my $contractor = $contract->contractor; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 86 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 87 | 49 | ▶ | croak "trying to contract function [$contractor] twice" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 88 | if ($self->has_contract($contractor)); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 89 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 90 | 49 | $index->{$contractor} = $contract; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 91 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 92 | 49 | return $self; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 93 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 94 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 95 | ################################################################ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 96 | # | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 97 | # | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 98 | # Operations on contracts during runtime | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 99 | # | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 100 | # | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 101 | ################################################################ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 102 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 103 | 1 | ▶ | sub enable_all_contracts { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
enable_all_contracts: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 104 | 1 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 105 | 1 | map { $_->enable } $self->list_all_contracts; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 106 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 107 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 108 | 1 | ▶ | sub disable_all_contracts { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
disable_all_contracts: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 109 | 1 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 110 | 1 | map { $_->disable } $self->list_all_contracts; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 111 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 112 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 113 | 0 | ▶ | sub enable_contracts_matching { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
enable_contracts_matching: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 114 | 0 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 115 | 0 | map { $_->enable } $self->find_contracts_matching(@_); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 116 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 117 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 118 | 0 | ▶ | sub disable_contracts_matching { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
disable_contracts_matching: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 119 | 0 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 120 | 0 | map { $_->disable } $self->find_contracts_matching(@_); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 121 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 122 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 123 | 50 | ▶ | sub find_contract { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
find_contract: called CC=5 SCAR=17.8
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 124 | 50 | my ($self, $contractor) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 125 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 126 | 50 | ▶ | croak "method find_contract() expects a fully qualified function name as argument" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition44%4/9
scalar @_ != 2 or not defined $contractor or ref $contractor ne "" or not $contractor =~ /::/
scalar @_ != 2 or not defined $contractor or ref $contractor ne ""
scalar @_ != 2 or not defined $contractor
MC/DC25%1/4
scalar @_ != 2 or not defined $contractor or ref $contractor ne "" or not $contractor =~ /::/
scalar @_ != 2
not defined $contractor
ref $contractor ne ""
not $contractor =~ /::/
Truth table40%2/5
A or not(B) or C or not(D)
A= scalar @_ != 2B= not defined $contractorC= ref $contractor ne ""D= not $contractor =~ /::/
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 127 | if ( scalar @_ != 2 || | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 128 | !defined $contractor || | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 129 | ref $contractor ne '' || | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 130 | $contractor !~ /::/ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 131 | ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 132 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 133 | 45 | my $index = $self->_contract_index; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 134 | 45 | return $index->{$contractor}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 135 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 136 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 137 | 10 | ▶ | sub find_contracts_matching { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
find_contracts_matching: called CC=6 SCAR=18.8
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 138 | 10 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 139 | 10 | my $match = shift; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 140 | 10 | my @contracts; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 141 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 142 | # TODO: fix croak level when called from enable/disable_matching | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 143 | # local $Carp::CarpLevel = 2 if ((caller(1))[3] =~ /^Sub::Contract::Pool::(enable|disable)_contracts_matching$/); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 144 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 145 | 10 | ▶ | croak "method find_contracts_matching() expects a regular expression" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%2/6
scalar @_ != 0 or not defined $match or ref $match ne ""
scalar @_ != 0 or not defined $match
MC/DC0%0/3
scalar @_ != 0 or not defined $match or ref $match ne ""
scalar @_ != 0
not defined $match
ref $match ne ""
Truth table25%1/4
A or not(B) or C
A= scalar @_ != 0B= not defined $matchC= ref $match ne ""
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 146 | if (scalar @_ != 0 || !defined $match || ref $match ne ''); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 147 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 148 | 10 | ▶ | while ( my ($name,$contract) = each %{$self->_contract_index} ) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 149 | 40 | ▶ | push @contracts, $contract if ($name =~ /^$match$/); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 150 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 151 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 152 | 10 | return @contracts; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 153 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 154 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 155 | 23 | 1; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 156 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 157 | __END__ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||