| 1 | package Class::Accessor::Array::Glob; | ||||||||||||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||||||||||||
| 3 | 1 | our $DATE = '2016-03-29'; # DATE | |||||||||||||||||||||||||||||||||||||
| 4 | 1 | our $VERSION = '0.01'; # VERSION | |||||||||||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||||||||||||
| 6 | 2 | ▶ | use 5.010001; | ||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||
| 7 | 2 | ▶ | use strict 'subs', 'vars'; | ||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||
| 8 | 2 | ▶ | use warnings; | ||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||||||||||||
| 10 | 2 | ▶ | sub import { | ||||||||||||||||||||||||||||||||||||
|
Subroutine
import: called CC=17 SCAR=30.6
| |||||||||||||||||||||||||||||||||||||||
| 11 | 2 | my ($class0, $spec) = @_; | |||||||||||||||||||||||||||||||||||||
| 12 | 2 | my $caller = caller(); | |||||||||||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||||||||||||
| 14 | 2 | ▶ | no warnings 'redefine'; | ||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||||||||||||
| 16 | 2 | my $max_idx; | |||||||||||||||||||||||||||||||||||||
| 17 | 2 | for (values %{$spec->{accessors}}) { | |||||||||||||||||||||||||||||||||||||
| 18 | 8 | ▶ | $max_idx = $_ if !defined($max_idx) || $max_idx < $_; | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
not defined $max_idx or $max_idx < $_
MC/DC100%2/2
not defined $max_idx or $max_idx < $_
not defined $max_idx
$max_idx < $_
Truth table100%3/3
not(A) or B
A= not defined $max_idxB= $max_idx < $_
| |||||||||||||||||||||||||||||||||||||||
| 19 | } | ||||||||||||||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||||||||||||
| 21 | 2 | my $glob_attribute = $spec->{glob_attribute}; | |||||||||||||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||||||||||||
| 23 | # generate accessors | ||||||||||||||||||||||||||||||||||||||
| 24 | 2 | for my $meth (keys %{$spec->{accessors}}) { | |||||||||||||||||||||||||||||||||||||
| 25 | 8 | my $idx = $spec->{accessors}{$meth}; | |||||||||||||||||||||||||||||||||||||
| 26 | 8 | my $is = 'rw'; | |||||||||||||||||||||||||||||||||||||
| 27 | 8 | ▶ | my $code_str = $is eq 'rw' ? 'sub (;$) { ' : 'sub () { '; | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 28 | 8 | ▶ | if (defined($glob_attribute) && $glob_attribute eq $meth) { | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
defined $glob_attribute and $glob_attribute eq $meth
MC/DC100%2/2
defined $glob_attribute and $glob_attribute eq $meth
defined $glob_attribute
$glob_attribute eq $meth
Truth table100%3/3
A and B
A= defined $glob_attributeB= $glob_attribute eq $meth
| |||||||||||||||||||||||||||||||||||||||
| 29 | 1 | ▶ | die "Glob attribute must be put at the last index" | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 30 | unless $idx == $max_idx; | ||||||||||||||||||||||||||||||||||||||
| 31 | 1 | ▶ | $code_str .= "splice(\@{\$_[0]}, $idx, scalar(\@{\$_[0]}), \@{\$_[1]}) if \@_ > 1; " | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 32 | if $is eq 'rw'; | ||||||||||||||||||||||||||||||||||||||
| 33 | 1 | $code_str .= "[ \@{\$_[0]}[$idx .. \$#{\$_[0]}] ]; "; | |||||||||||||||||||||||||||||||||||||
| 34 | } else { | ||||||||||||||||||||||||||||||||||||||
| 35 | 7 | ▶ | $code_str .= "\$_[0][$idx] = \$_[1] if \@_ > 1; " | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 36 | if $is eq 'rw'; | ||||||||||||||||||||||||||||||||||||||
| 37 | 7 | $code_str .= "\$_[0][$idx]; "; | |||||||||||||||||||||||||||||||||||||
| 38 | } | ||||||||||||||||||||||||||||||||||||||
| 39 | 8 | $code_str .= "}"; | |||||||||||||||||||||||||||||||||||||
| 40 | #say "D:accessor code for $meth: ", $code_str; | ||||||||||||||||||||||||||||||||||||||
| 41 | 0 | ▶ | *{"$caller\::$meth"} = eval $code_str; | ||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=2 SCAR=8.5
Subroutine
__ANON__: called CC=2 SCAR=8.5
Subroutine
__ANON__: not called CC=2 SCAR=8.5
Subroutine
__ANON__: called CC=2 SCAR=8.5
Subroutine
__ANON__: not called CC=2 SCAR=8.5
Subroutine
__ANON__: called CC=2 SCAR=8.5
Subroutine
__ANON__: called CC=2 SCAR=8.5
Subroutine
__ANON__: called CC=2 SCAR=8.5
Branch: 8 branches
| |||||||||||||||||||||||||||||||||||||||
| 42 | 8 | ▶ | die if $@; | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 43 | } | ||||||||||||||||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||||||||||||||||
| 45 | # generate constructor | ||||||||||||||||||||||||||||||||||||||
| 46 | { | ||||||||||||||||||||||||||||||||||||||
| 47 | 2 | ▶ | my $n = ($max_idx // 0) + 1; $n-- if defined $glob_attribute; | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition50%1/2
$max_idx // 0
MC/DC0%0/1
$max_idx // 0
$max_idx
Truth table50%1/2
A
A= $max_idx
| |||||||||||||||||||||||||||||||||||||||
| 48 | 2 | my $code_str = 'sub { my $class = shift; bless [(undef) x '.$n.'], $class }'; | |||||||||||||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||||||||||||
| 50 | #say "D:constructor code for class $caller: ", $code_str; | ||||||||||||||||||||||||||||||||||||||
| 51 | 2 | ▶ | my $constructor = $spec->{constructor} || "new"; | ||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$spec->{'constructor'} || 'new'
MC/DC0%0/1
$spec->{'constructor'} || 'new'
$spec->{'constructor'}
Truth table50%1/2
A
A= $spec->{'constructor'}
| |||||||||||||||||||||||||||||||||||||||
| 52 | 2 | ▶ | unless (*{"$caller\::$constructor"}{CODE}) { | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 53 | 2 | ▶ | *{"$caller\::$constructor"} = eval $code_str; | ||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||
| 54 | 2 | ▶ | die if $@; | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 55 | }; | ||||||||||||||||||||||||||||||||||||||
| 56 | } | ||||||||||||||||||||||||||||||||||||||
| 57 | } | ||||||||||||||||||||||||||||||||||||||
| 58 | |||||||||||||||||||||||||||||||||||||||
| 59 | 1 | 1; | |||||||||||||||||||||||||||||||||||||
| 60 | # ABSTRACT: Generate accessors/constructor for array-based object (supports globbing attribute) | ||||||||||||||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||||||||||||
| 62 | __END__ | ||||||||||||||||||||||||||||||||||||||