| 1 | package Class::Member::GLOB; | ||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||
| 3 | 2 | ▶ | use strict; | ||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||
| 4 | 2 | our $VERSION='1.6'; | |||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||
| 6 | 2 | ▶ | use Carp 'confess'; | ||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||
| 7 | 2 | ▶ | use IO::Handle; | ||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||
| 9 | 2 | my $get_class_members; | |||||||||||||||||||||||||
| 10 | 4 | ▶ | $get_class_members=sub { | ||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||
| 11 | 4 | my $pack=$_[0]; | |||||||||||||||||||||||||
| 12 | 2 | ▶ | no strict 'refs'; | ||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||
| 13 | 4 | my %cm; | |||||||||||||||||||||||||
| 14 | 4 | @cm{@{$pack.'::CLASS_MEMBERS'}}=(); | |||||||||||||||||||||||||
| 15 | 4 | foreach my $p (@{$pack.'::ISA'}) { | |||||||||||||||||||||||||
| 16 | 2 | @cm{$get_class_members->($p)}=(); | |||||||||||||||||||||||||
| 17 | } | ||||||||||||||||||||||||||
| 18 | 4 | return keys %cm; | |||||||||||||||||||||||||
| 19 | 2 | }; | |||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||
| 21 | 3 | ▶ | sub import { | ||||||||||||||||||||||||
|
Subroutine
import: called CC=8 SCAR=20.9
| |||||||||||||||||||||||||||
| 22 | 3 | my $pack=shift; | |||||||||||||||||||||||||
| 23 | 3 | ($pack)=caller; | |||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||
| 25 | 26 | ▶ | my $getset=sub : lvalue { | ||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||
| 26 | 26 | my $I=shift; | |||||||||||||||||||||||||
| 27 | 26 | my $what=shift; | |||||||||||||||||||||||||
| 28 | 26 | ▶ | unless( UNIVERSAL::isa( $I, 'GLOB' ) ) { | ||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||
| 29 | 1 | confess "$pack\::$what must be called as instance method\n"; | |||||||||||||||||||||||||
| 30 | } | ||||||||||||||||||||||||||
| 31 | 25 | $what=$pack.'::'.$what; | |||||||||||||||||||||||||
| 32 | 25 | ▶ | if( $#_>=0 ) { | ||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||
| 33 | 2 | ${*$I}{$what}=shift; | |||||||||||||||||||||||||
| 34 | } | ||||||||||||||||||||||||||
| 35 | 25 | ${*$I}{$what}; | |||||||||||||||||||||||||
| 36 | 3 | }; | |||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||
| 38 | 2 | ▶ | my $new=sub { | ||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=7 SCAR=19.5
| |||||||||||||||||||||||||||
| 39 | 2 | my $parent=shift; | |||||||||||||||||||||||||
| 40 | 2 | ▶ | my $class=ref($parent) || $parent; | ||||||||||||||||||||||||
|
Condition66%2/3
ref $parent || $parent
MC/DC0%0/2
ref $parent || $parent
ref $parent
$parent
Truth table66%2/3
A || B
A= ref $parentB= $parent
| |||||||||||||||||||||||||||
| 41 | |||||||||||||||||||||||||||
| 42 | 2 | my $I=bless IO::Handle->new=>$class; | |||||||||||||||||||||||||
| 43 | 2 | my %o=@_; | |||||||||||||||||||||||||
| 44 | 2 | my @CLASS_MEMBERS=$get_class_members->($class); | |||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||
| 46 | 2 | ▶ | if( ref($parent) ) { # inherit first | ||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||
| 47 | 1 | foreach my $m (@CLASS_MEMBERS) { | |||||||||||||||||||||||||
| 48 | 3 | $I->$m=$parent->$m; | |||||||||||||||||||||||||
| 49 | } | ||||||||||||||||||||||||||
| 50 | } | ||||||||||||||||||||||||||
| 51 | |||||||||||||||||||||||||||
| 52 | # then override with named parameters | ||||||||||||||||||||||||||
| 53 | 2 | foreach my $m (@CLASS_MEMBERS) { | |||||||||||||||||||||||||
| 54 | 6 | ▶ | $I->$m=$o{$m} if( exists $o{$m} ); | ||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||
| 55 | } | ||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||
| 57 | 2 | my $init=$I->can('I N I T'); | |||||||||||||||||||||||||
| 58 | 2 | ▶ | if( $init ) { | ||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||
| 59 | 2 | $init=$init->(); | |||||||||||||||||||||||||
| 60 | 2 | $I->$init; | |||||||||||||||||||||||||
| 61 | } | ||||||||||||||||||||||||||
| 62 | |||||||||||||||||||||||||||
| 63 | 2 | return $I; | |||||||||||||||||||||||||
| 64 | 3 | }; | |||||||||||||||||||||||||
| 65 | |||||||||||||||||||||||||||
| 66 | 3 | foreach my $name (@_) { | |||||||||||||||||||||||||
| 67 | 10 | ▶ | if( $name=~/^-(.*)/ ) { # reserved name, aka option | ||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||
| 68 | 5 | my $o=$1; | |||||||||||||||||||||||||
| 69 | 5 | ▶ | if( $o eq 'CLASS_MEMBERS' ) { | ||||||||||||||||||||||||
|
Branch: 5 branches
| |||||||||||||||||||||||||||
| 70 | 3 | local $_; | |||||||||||||||||||||||||
| 71 | 2 | ▶ | no strict 'refs'; | ||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||
| 72 | 3 | *{$pack.'::CLASS_MEMBERS'}=[grep {!/^-/} @_]; | |||||||||||||||||||||||||
| 73 | } elsif( $o=~/^NEW=(.+)/ ) { | ||||||||||||||||||||||||||
| 74 | 2 | ▶ | no strict 'refs'; | ||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||
| 75 | 0 | *{$pack.'::'.$1}=$new; | |||||||||||||||||||||||||
| 76 | } elsif( $o eq 'NEW' ) { | ||||||||||||||||||||||||||
| 77 | 2 | ▶ | no strict 'refs'; | ||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||
| 78 | 1 | *{$pack.'::new'}=$new; | |||||||||||||||||||||||||
| 79 | } elsif( $o=~/^INIT=(.+)/ ) { | ||||||||||||||||||||||||||
| 80 | 2 | ▶ | no strict 'refs'; | ||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||
| 81 | 1 | my $init="$1"; | |||||||||||||||||||||||||
| 82 | 0 | ▶ | *{$pack.'::I N I T'}=sub(){$init}; | ||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=0.4
| |||||||||||||||||||||||||||
| 83 | } elsif( $o eq 'INIT' ) { | ||||||||||||||||||||||||||
| 84 | 2 | ▶ | no strict 'refs'; | ||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||
| 85 | 0 | *{$pack.'::I N I T'}=sub(){"init"}; | |||||||||||||||||||||||||
| 86 | } | ||||||||||||||||||||||||||
| 87 | } else { | ||||||||||||||||||||||||||
| 88 | 2 | ▶ | no strict 'refs'; | ||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||
| 89 | 5 | ▶ | *{$pack.'::'.$name}=sub:lvalue { | ||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||
| 90 | 26 | my $I=shift; | |||||||||||||||||||||||||
| 91 | 26 | &{$getset}( $I, $name, @_ ); | |||||||||||||||||||||||||
| 92 | 5 | }; | |||||||||||||||||||||||||
| 93 | } | ||||||||||||||||||||||||||
| 94 | } | ||||||||||||||||||||||||||
| 95 | } | ||||||||||||||||||||||||||
| 96 | |||||||||||||||||||||||||||
| 97 | 2 | 1; | |||||||||||||||||||||||||
| 98 | |||||||||||||||||||||||||||
| 99 | __END__ | ||||||||||||||||||||||||||