| 1 | package NG::Class; | ||||||||||||||||||||
| 2 | 1 | ▶ | use strict; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 3 | 1 | ▶ | use warnings; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 4 | 1 | ▶ | use Data::Dumper; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 5 | |||||||||||||||||||||
| 6 | =head2 def | ||||||||||||||||||||
| 7 | def_class Animal => ['sex', 'leg_color'] => { | ||||||||||||||||||||
| 8 | sound => sub { .... }, | ||||||||||||||||||||
| 9 | run => sub { .... }, | ||||||||||||||||||||
| 10 | }; | ||||||||||||||||||||
| 11 | |||||||||||||||||||||
| 12 | def_class Dog => Animal => ['head_color'] => { | ||||||||||||||||||||
| 13 | eat => sub { ... }, | ||||||||||||||||||||
| 14 | run => sub { ... }, # override method | ||||||||||||||||||||
| 15 | }; | ||||||||||||||||||||
| 16 | |||||||||||||||||||||
| 17 | my $x = Dog->new; | ||||||||||||||||||||
| 18 | $x->eat('bone'); | ||||||||||||||||||||
| 19 | =cut | ||||||||||||||||||||
| 20 | 5 | ▶ | sub def { | ||||||||||||||||||
|
Subroutine
def: called CC=6 SCAR=22.5
Pod
documented
| |||||||||||||||||||||
| 21 | 5 | my $class = shift; | |||||||||||||||||||
| 22 | 5 | my $methods = pop; | |||||||||||||||||||
| 23 | 5 | my $attrs = pop; | |||||||||||||||||||
| 24 | 5 | ▶ | my $parent = shift || 'NG::Object'; | ||||||||||||||||||
|
Condition100%2/2
shift() || 'NG::Object'
MC/DC100%1/1
shift() || 'NG::Object'
shift()
Truth table100%2/2
A
A= shift()
| |||||||||||||||||||||
| 25 | |||||||||||||||||||||
| 26 | |||||||||||||||||||||
| 27 | 5 | eval "package $class"; | |||||||||||||||||||
| 28 | 1 | ▶ | eval "use $parent"; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
Subroutine
BEGIN: called CC=1 SCAR=0.0
Subroutine
BEGIN: called CC=1 SCAR=0.0
Subroutine
BEGIN: called CC=1 SCAR=0.0
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 29 | 5 | ▶ | if( $parent ne 'undef' ) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 30 | 5 | *t = eval('*'.$class.'::ISA'); | |||||||||||||||||||
| 31 | 5 | *t = [$parent]; | |||||||||||||||||||
| 32 | } | ||||||||||||||||||||
| 33 | |||||||||||||||||||||
| 34 | 5 | for my $method ( keys %{$methods} ) { | |||||||||||||||||||
| 35 | 28 | ▶ | if(ref($methods->{$method}) eq 'CODE'){ | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 36 | 28 | *t = eval('*'.$class.'::'.$method); | |||||||||||||||||||
| 37 | 28 | *t = $methods->{$method}; | |||||||||||||||||||
| 38 | } | ||||||||||||||||||||
| 39 | } | ||||||||||||||||||||
| 40 | |||||||||||||||||||||
| 41 | 5 | for my $attr ( @{$attrs} ) { | |||||||||||||||||||
| 42 | 1 | *t = eval('*'.$class.'::'.$attr); | |||||||||||||||||||
| 43 | 0 | ▶ | *t = sub :lvalue { | ||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||
| 44 | 0 | shift->{$attr}; | |||||||||||||||||||
| 45 | 1 | }; | |||||||||||||||||||
| 46 | } | ||||||||||||||||||||
| 47 | |||||||||||||||||||||
| 48 | 5 | *t = eval('*'.$class.'::meta'); | |||||||||||||||||||
| 49 | 0 | ▶ | *t = sub { | ||||||||||||||||||
|
Subroutine
__ANON__: not called CC=4 SCAR=30.0
| |||||||||||||||||||||
| 50 | 0 | my (@attr_list, @method_list, %seen); | |||||||||||||||||||
| 51 | 0 | ▶ | if( $parent ne 'undef' ) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 52 | 0 | ▶ | push @attr_list, @{ $parent->meta->{attrs} } if $parent->meta->{attrs}; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 53 | 0 | ▶ | push @method_list, @{ $parent->meta->{methods} } if $parent->meta->{methods}; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 54 | } | ||||||||||||||||||||
| 55 | 0 | push @attr_list, @{$attrs}; | |||||||||||||||||||
| 56 | 0 | push @method_list, keys %{$methods}; | |||||||||||||||||||
| 57 | 0 | return { attrs => [ grep { not $seen{$_}++ } @attr_list ], | |||||||||||||||||||
| 58 | 0 | methods => [ grep { not $seen{$_}++ } @method_list ], | |||||||||||||||||||
| 59 | }; | ||||||||||||||||||||
| 60 | 5 | }; | |||||||||||||||||||
| 61 | |||||||||||||||||||||
| 62 | 5 | *t = eval('*'.$class.'::new'); | |||||||||||||||||||
| 63 | 0 | ▶ | *t = sub { | ||||||||||||||||||
|
Subroutine
__ANON__: not called CC=4 SCAR=30.0
| |||||||||||||||||||||
| 64 | 0 | my ($class, @args) = @_; | |||||||||||||||||||
| 65 | 0 | ▶ | push @args, '' if $#args % 2 == 0; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 66 | 0 | ▶ | my $o = bless {@args}, ref $class || $class; | ||||||||||||||||||
|
Condition0%0/3
ref $class || $class
MC/DC0%0/2
ref $class || $class
ref $class
$class
Truth table0%0/3
A || B
A= ref $classB= $class
| |||||||||||||||||||||
| 67 | 0 | ▶ | if(defined $methods->{build}){ | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 68 | 0 | $o->build(@args); | |||||||||||||||||||
| 69 | } | ||||||||||||||||||||
| 70 | 0 | $o; | |||||||||||||||||||
| 71 | } | ||||||||||||||||||||
| 72 | |||||||||||||||||||||
| 73 | 5 | } | |||||||||||||||||||
| 74 | |||||||||||||||||||||
| 75 | 1 | 1; | |||||||||||||||||||