| 1 | package Object::Factory::Declarative; | ||||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||||
| 3 | 1 | ▶ | use 5.006; | ||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||
| 4 | 1 | ▶ | use strict; | ||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||
| 5 | 1 | ▶ | use warnings; | ||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||
| 6 | 1 | ▶ | use Carp; | ||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||||
| 8 | 1 | our @ISA = qw(); | |||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||||
| 10 | 1 | our $VERSION = '0.07'; | |||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||||
| 12 | 1 | my @decl_keys = qw(constructor constructor_args method method_args package); | |||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||||
| 14 | sub expand_scalar_arg | ||||||||||||||||||||||||||||||
| 15 | 3 | ▶ | { | ||||||||||||||||||||||||||||
|
Subroutine
expand_scalar_arg: called CC=3 SCAR=11.3
Pod
undocumented
| |||||||||||||||||||||||||||||||
| 16 | 3 | my ($obj, $name, $arg) = @_; | |||||||||||||||||||||||||||||
| 17 | 3 | my @res = ($arg); | |||||||||||||||||||||||||||||
| 18 | 3 | ▶ | if(@_>3) | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 19 | { | ||||||||||||||||||||||||||||||
| 20 | 3 | my $opt = $_[3]; | |||||||||||||||||||||||||||||
| 21 | 1 | ▶ | eval { no warnings; @res = $obj->$arg($opt, $name); }; | ||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||
| 22 | } | ||||||||||||||||||||||||||||||
| 23 | else | ||||||||||||||||||||||||||||||
| 24 | { | ||||||||||||||||||||||||||||||
| 25 | 0 | ▶ | eval { no warnings; @res = $obj->$arg($name); }; | ||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.6
| |||||||||||||||||||||||||||||||
| 26 | } | ||||||||||||||||||||||||||||||
| 27 | 3 | ▶ | wantarray?@res:pop @res; | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 28 | } | ||||||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||||
| 30 | sub expand_array_arg | ||||||||||||||||||||||||||||||
| 31 | 0 | ▶ | { | ||||||||||||||||||||||||||||
|
Subroutine
expand_array_arg: not called CC=3 SCAR=24.8
Pod
undocumented
| |||||||||||||||||||||||||||||||
| 32 | 0 | my ($obj, $name, @args) = @_; | |||||||||||||||||||||||||||||
| 33 | 0 | my @res; | |||||||||||||||||||||||||||||
| 34 | 0 | push @res, expand_scalar_arg($obj, $name, $_) foreach @args; | |||||||||||||||||||||||||||||
| 35 | 0 | ▶ | wantarray?@res:pop @res; | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 36 | } | ||||||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||||||
| 38 | sub expand_hash_arg | ||||||||||||||||||||||||||||||
| 39 | 2 | ▶ | { | ||||||||||||||||||||||||||||
|
Subroutine
expand_hash_arg: called CC=4 SCAR=14.2
Pod
undocumented
| |||||||||||||||||||||||||||||||
| 40 | 2 | my ($obj, $name, @args) = @_; | |||||||||||||||||||||||||||||
| 41 | 2 | ▶ | return expand_array_arg($obj, $name, @args) if @args&1; | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 42 | 2 | my @res; | |||||||||||||||||||||||||||||
| 43 | 2 | my %args = @args; | |||||||||||||||||||||||||||||
| 44 | push @res, $_, expand_scalar_arg($obj, $name, $args{$_}, $_) | ||||||||||||||||||||||||||||||
| 45 | 2 | foreach keys %args; | |||||||||||||||||||||||||||||
| 46 | 2 | ▶ | wantarray?@res:pop @res; | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 47 | } | ||||||||||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||||||||
| 49 | sub expand_credentials | ||||||||||||||||||||||||||||||
| 50 | 2 | ▶ | { | ||||||||||||||||||||||||||||
|
Subroutine
expand_credentials: called CC=5 SCAR=16.5
Pod
undocumented
| |||||||||||||||||||||||||||||||
| 51 | 2 | my ($argf) = @_; | |||||||||||||||||||||||||||||
| 52 | 2 | ▶ | return sub {} unless defined $argf; | ||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=0.4
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 53 | 2 | ▶ | return (\&expand_scalar_arg, $argf) unless ref $argf; | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 54 | 2 | my @res = ($argf); | |||||||||||||||||||||||||||||
| 55 | 1 | ▶ | eval { no warnings; @res = @$argf; }; | ||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||
| 56 | 2 | ▶ | return (\&expand_array_arg, @res) unless $@; | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 57 | 1 | ▶ | eval { no warnings; @res = %$argf; }; | ||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||
| 58 | 2 | ▶ | return (\&expand_hash_arg, @res) unless $@; | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 59 | 0 | return (\&expand_scalar_arg, @res); | |||||||||||||||||||||||||||||
| 60 | } | ||||||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||||
| 62 | sub generate_method | ||||||||||||||||||||||||||||||
| 63 | 1 | ▶ | { | ||||||||||||||||||||||||||||
|
Subroutine
generate_method: called CC=7 SCAR=30.8
Pod
undocumented
| |||||||||||||||||||||||||||||||
| 64 | 1 | my ($class, $name, $cons, $c_args, $init, $i_args) = @_; | |||||||||||||||||||||||||||||
| 65 | 1 | ▶ | my $package = ref $class || $class; | ||||||||||||||||||||||||||||
|
Condition33%1/3
ref $class || $class
MC/DC0%0/2
ref $class || $class
ref $class
$class
Truth table33%1/3
A || B
A= ref $classB= $class
| |||||||||||||||||||||||||||||||
| 66 | 1 | my $fullname = $package . '::' . $name; | |||||||||||||||||||||||||||||
| 67 | 1 | my ($cons_expand_func, @c_args) = expand_credentials($c_args); | |||||||||||||||||||||||||||||
| 68 | 1 | my ($init_expand_func, @i_args) = expand_credentials($i_args); | |||||||||||||||||||||||||||||
| 69 | 1 | ▶ | no strict 'refs'; | ||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||
| 70 | # We have several similar cases... | ||||||||||||||||||||||||||||||
| 71 | # Case 1a & 1b - no init method | ||||||||||||||||||||||||||||||
| 72 | 1 | ▶ | unless($init) | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 73 | { | ||||||||||||||||||||||||||||||
| 74 | # 1a - with constructor args | ||||||||||||||||||||||||||||||
| 75 | 0 | ▶ | if(@c_args) | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 76 | { | ||||||||||||||||||||||||||||||
| 77 | *$fullname = sub | ||||||||||||||||||||||||||||||
| 78 | 0 | ▶ | { | ||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||||
| 79 | 0 | my ($obj) = @_; | |||||||||||||||||||||||||||||
| 80 | 0 | $obj->$cons(&{$cons_expand_func}($obj, $name, @c_args)); | |||||||||||||||||||||||||||||
| 81 | 0 | } ; | |||||||||||||||||||||||||||||
| 82 | } | ||||||||||||||||||||||||||||||
| 83 | # 2a - without constructor args | ||||||||||||||||||||||||||||||
| 84 | else | ||||||||||||||||||||||||||||||
| 85 | { | ||||||||||||||||||||||||||||||
| 86 | *$fullname = sub | ||||||||||||||||||||||||||||||
| 87 | 0 | ▶ | { | ||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||||
| 88 | 0 | my ($obj) = @_; | |||||||||||||||||||||||||||||
| 89 | 0 | $obj->$cons; | |||||||||||||||||||||||||||||
| 90 | 0 | } ; | |||||||||||||||||||||||||||||
| 91 | } | ||||||||||||||||||||||||||||||
| 92 | 0 | return; | |||||||||||||||||||||||||||||
| 93 | } | ||||||||||||||||||||||||||||||
| 94 | # Case 2a & 2b - init method, no init args | ||||||||||||||||||||||||||||||
| 95 | 1 | ▶ | unless(@i_args) | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 96 | { | ||||||||||||||||||||||||||||||
| 97 | # 2a - with constructor args | ||||||||||||||||||||||||||||||
| 98 | 0 | ▶ | if(@c_args) | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 99 | { | ||||||||||||||||||||||||||||||
| 100 | *$fullname = sub | ||||||||||||||||||||||||||||||
| 101 | 0 | ▶ | { | ||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=2 SCAR=17.9
| |||||||||||||||||||||||||||||||
| 102 | 0 | my ($obj, @args) = @_; | |||||||||||||||||||||||||||||
| 103 | 0 | my $rv = $obj->$cons(&{$cons_expand_func}($obj, | |||||||||||||||||||||||||||||
| 104 | $name, @c_args)); | ||||||||||||||||||||||||||||||
| 105 | 0 | $rv->$init; | |||||||||||||||||||||||||||||
| 106 | # expand_hash_arg will convert to expand_array_arg... | ||||||||||||||||||||||||||||||
| 107 | 0 | ▶ | $rv->$init(expand_hash_arg($obj, $name, @args)) if @args; | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 108 | 0 | $rv; | |||||||||||||||||||||||||||||
| 109 | 0 | } ; | |||||||||||||||||||||||||||||
| 110 | } | ||||||||||||||||||||||||||||||
| 111 | # 2b - without constructor args | ||||||||||||||||||||||||||||||
| 112 | else | ||||||||||||||||||||||||||||||
| 113 | { | ||||||||||||||||||||||||||||||
| 114 | *$fullname = sub | ||||||||||||||||||||||||||||||
| 115 | 0 | ▶ | { | ||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=2 SCAR=17.9
| |||||||||||||||||||||||||||||||
| 116 | 0 | my ($obj, @args) = @_; | |||||||||||||||||||||||||||||
| 117 | 0 | my $rv = $obj->$cons; | |||||||||||||||||||||||||||||
| 118 | 0 | $rv->$init; | |||||||||||||||||||||||||||||
| 119 | # expand_hash_arg will convert to expand_array_arg... | ||||||||||||||||||||||||||||||
| 120 | 0 | ▶ | $rv->$init(expand_hash_arg($rv, $name, @args)) if @args; | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 121 | 0 | $rv; | |||||||||||||||||||||||||||||
| 122 | 0 | } ; | |||||||||||||||||||||||||||||
| 123 | } | ||||||||||||||||||||||||||||||
| 124 | 0 | return; | |||||||||||||||||||||||||||||
| 125 | } | ||||||||||||||||||||||||||||||
| 126 | # Case 3a & 3b - init with args | ||||||||||||||||||||||||||||||
| 127 | # 3a - with constructor args | ||||||||||||||||||||||||||||||
| 128 | 1 | ▶ | if(@c_args) | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 129 | { | ||||||||||||||||||||||||||||||
| 130 | *$fullname = sub | ||||||||||||||||||||||||||||||
| 131 | 1 | ▶ | { | ||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=7.1
| |||||||||||||||||||||||||||||||
| 132 | 1 | my ($obj, @args) = @_; | |||||||||||||||||||||||||||||
| 133 | 1 | my $rv = $obj->$cons(&{$cons_expand_func}($obj, | |||||||||||||||||||||||||||||
| 134 | $name, @c_args)); | ||||||||||||||||||||||||||||||
| 135 | 1 | $rv->$init(&{$init_expand_func}($obj, $name, @i_args)); | |||||||||||||||||||||||||||||
| 136 | 0 | ▶ | $rv->$init(&{$init_expand_func}($obj, $name, @args)) if @args; | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 137 | 1 | $rv; | |||||||||||||||||||||||||||||
| 138 | 1 | } ; | |||||||||||||||||||||||||||||
| 139 | } | ||||||||||||||||||||||||||||||
| 140 | # 3b - without constructor args | ||||||||||||||||||||||||||||||
| 141 | else | ||||||||||||||||||||||||||||||
| 142 | { | ||||||||||||||||||||||||||||||
| 143 | *$fullname = sub | ||||||||||||||||||||||||||||||
| 144 | 0 | ▶ | { | ||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=2 SCAR=17.9
| |||||||||||||||||||||||||||||||
| 145 | 0 | my ($obj, @args) = @_; | |||||||||||||||||||||||||||||
| 146 | 0 | my $rv = $obj->$cons; | |||||||||||||||||||||||||||||
| 147 | 0 | $rv->$init(&{$init_expand_func}($obj, $name, @i_args)); | |||||||||||||||||||||||||||||
| 148 | 0 | ▶ | $rv->$init(&{$init_expand_func}($obj, $name, @args)) if @args; | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 149 | 0 | $rv; | |||||||||||||||||||||||||||||
| 150 | 0 | } ; | |||||||||||||||||||||||||||||
| 151 | } | ||||||||||||||||||||||||||||||
| 152 | } | ||||||||||||||||||||||||||||||
| 153 | |||||||||||||||||||||||||||||||
| 154 | sub import | ||||||||||||||||||||||||||||||
| 155 | 2 | ▶ | { | ||||||||||||||||||||||||||||
|
Subroutine
import: called CC=16 SCAR=31.0
| |||||||||||||||||||||||||||||||
| 156 | 2 | my ($package, @args) = @_; | |||||||||||||||||||||||||||||
| 157 | 2 | ▶ | carp "Expected an even number of arguments" and return if @args&1; | ||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition25%1/4
@args & 1 && (carp('Expected an even number of arguments') && (return))
carp("Expected an even number of arguments") and return
MC/DC0%0/2
@args & 1 && (carp('Expected an even number of arguments') && (return))
@args & 1
carp("Expected an even number of arguments")
Truth table33%1/3
A && B
A= @args & 1B= carp("Expected an even number of arguments")
| |||||||||||||||||||||||||||||||
| 158 | 2 | my $callpkg = caller; | |||||||||||||||||||||||||||||
| 159 | 2 | my %defaults = | |||||||||||||||||||||||||||||
| 160 | ( | ||||||||||||||||||||||||||||||
| 161 | package => $callpkg, | ||||||||||||||||||||||||||||||
| 162 | ) ; | ||||||||||||||||||||||||||||||
| 163 | 2 | ▶ | while(@args) | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 164 | { | ||||||||||||||||||||||||||||||
| 165 | 2 | my ($name, $ref) = splice @args, 0, 2; | |||||||||||||||||||||||||||||
| 166 | 2 | my %h; | |||||||||||||||||||||||||||||
| 167 | 2 | ▶ | if('--defaults' eq $name) | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 168 | { | ||||||||||||||||||||||||||||||
| 169 | 1 | %defaults = ( package => $callpkg ); | |||||||||||||||||||||||||||||
| 170 | 1 | %h = %$ref; | |||||||||||||||||||||||||||||
| 171 | 1 | foreach my $k (grep { exists $h{$_}; } @decl_keys) | |||||||||||||||||||||||||||||
| 172 | { | ||||||||||||||||||||||||||||||
| 173 | 4 | delete $defaults{$k}; | |||||||||||||||||||||||||||||
| 174 | 4 | my $v = delete $h{$k}; | |||||||||||||||||||||||||||||
| 175 | 4 | ▶ | $defaults{$k} = $v if defined $v; | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 176 | } | ||||||||||||||||||||||||||||||
| 177 | 1 | ▶ | carp "Unexpected declaration key(s) ", join(',', keys %h) if %h; | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 178 | 1 | next; | |||||||||||||||||||||||||||||
| 179 | } | ||||||||||||||||||||||||||||||
| 180 | 1 | ▶ | if('--export-to' eq $name) | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 181 | { | ||||||||||||||||||||||||||||||
| 182 | 1 | ▶ | no strict 'refs'; | ||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||
| 183 | 0 | *$ref = \&generate_method; | |||||||||||||||||||||||||||||
| 184 | 0 | next; | |||||||||||||||||||||||||||||
| 185 | } | ||||||||||||||||||||||||||||||
| 186 | 1 | %h = %$ref; | |||||||||||||||||||||||||||||
| 187 | 1 | my %p; | |||||||||||||||||||||||||||||
| 188 | 1 | ▶ | $p{$_} = delete $h{$_} || $defaults{$_} foreach @decl_keys; | ||||||||||||||||||||||||||||
|
Condition66%2/3
delete $h{$_} || $defaults{$_}
MC/DC0%0/2
delete $h{$_} || $defaults{$_}
delete $h{$_}
$defaults{$_}
Truth table66%2/3
A || B
A= delete $h{$_}B= $defaults{$_}
| |||||||||||||||||||||||||||||||
| 189 | 1 | ▶ | carp "Unexpected declaration key(s) ", join(',', keys %h) if %h; | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 190 | carp "Can't have initialization args without an initialization method" | ||||||||||||||||||||||||||||||
| 191 | 1 | ▶ | if $p{method_args} and not $p{method}; | ||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$p{"method_args"} and not $p{"method"}
MC/DC0%0/2
$p{"method_args"} and not $p{"method"}
$p{"method_args"}
not $p{"method"}
Truth table33%1/3
A and not(B)
A= $p{"method_args"}B= not $p{"method"}
| |||||||||||||||||||||||||||||||
| 192 | 1 | ▶ | carp "Missing constructor" and next unless $p{constructor}; | ||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition25%1/4
$p{'constructor'} || carp('Missing constructor') && (next)
carp("Missing constructor") and next
MC/DC0%0/2
$p{'constructor'} || carp('Missing constructor') && (next)
$p{'constructor'}
carp("Missing constructor")
Truth table33%1/3
A || B
A= $p{'constructor'}B= carp("Missing constructor")
| |||||||||||||||||||||||||||||||
| 193 | 1 | generate_method($p{package}, $name, @p{qw(constructor constructor_args | |||||||||||||||||||||||||||||
| 194 | method method_args)}); | ||||||||||||||||||||||||||||||
| 195 | } | ||||||||||||||||||||||||||||||
| 196 | } | ||||||||||||||||||||||||||||||
| 197 | |||||||||||||||||||||||||||||||
| 198 | 1 | 1; | |||||||||||||||||||||||||||||
| 199 | __END__ | ||||||||||||||||||||||||||||||