| 1 | package Class::AccessorMaker; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | ## | ||||||||||||||||||||||||||||
| 4 | ## Class::AccessorMaker by Hartog "Sinister" de Mik | ||||||||||||||||||||||||||||
| 5 | ## | ||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||
| 7 | 4 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | 4 | ▶ | no strict 'refs'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||
| 10 | 4 | our $VERSION = "1.11"; | |||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||
| 12 | 4 | ▶ | use Carp; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||
| 14 | 6 | ▶ | sub import { | ||||||||||||||||||||||||||
|
Subroutine
import: called CC=10 SCAR=23.7
| |||||||||||||||||||||||||||||
| 15 | 6 | my ($class, $subs, $xtra) = @_; | |||||||||||||||||||||||||||
| 16 | 6 | ▶ | my $pkg = ref(caller) || caller; | ||||||||||||||||||||||||||
|
Condition33%1/3
ref caller || caller
MC/DC0%0/2
ref caller || caller
ref caller
caller
Truth table33%1/3
A || B
A= ref callerB= caller
| |||||||||||||||||||||||||||||
| 17 | 6 | ▶ | $xtra ||=""; | ||||||||||||||||||||||||||
|
Condition100%2/2
$xtra ||= ""
MC/DC100%1/1
$xtra ||= ""
$xtra
Truth table100%2/2
A
A= $xtra
| |||||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||
| 19 | 6 | ▶ | croak "Can't make methods out of an empty hash-ref\n" if !defined $subs; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||
| 21 | ## define a constructor | ||||||||||||||||||||||||||||
| 22 | 6 | ▶ | if ( $xtra ne "no_new" ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 23 | # we have a green light for 'new()' creation | ||||||||||||||||||||||||||||
| 24 | 5 | ▶ | if ( $xtra ne "new_init" ) { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 25 | 4 | ▶ | *{"${pkg}::new"} = sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=3 SCAR=11.1
| |||||||||||||||||||||||||||||
| 26 | 4 | ▶ | my $class = ref($_[0]) || $_[0]; shift; | ||||||||||||||||||||||||||
|
Condition33%1/3
ref $_[0] || $_[0]
MC/DC0%0/2
ref $_[0] || $_[0]
ref $_[0]
$_[0]
Truth table33%1/3
A || B
A= ref $_[0]B= $_[0]
| |||||||||||||||||||||||||||||
| 27 | 4 | my $self = bless({}, $class); | |||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||
| 29 | 4 | ▶ | while ( @_ ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 30 | 3 | my ($sub, $value) = (shift, shift); | |||||||||||||||||||||||||||
| 31 | 3 | $self->$sub($value); | |||||||||||||||||||||||||||
| 32 | } | ||||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||
| 34 | 4 | return $self; | |||||||||||||||||||||||||||
| 35 | 4 | }; | |||||||||||||||||||||||||||
| 36 | } elsif ( $xtra eq "new_init" ) { | ||||||||||||||||||||||||||||
| 37 | 1 | ▶ | *{"${pkg}::new"} = sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=3 SCAR=12.3
| |||||||||||||||||||||||||||||
| 38 | 1 | ▶ | my $class = ref($_[0]) || $_[0]; shift; | ||||||||||||||||||||||||||
|
Condition33%1/3
ref $_[0] || $_[0]
MC/DC0%0/2
ref $_[0] || $_[0]
ref $_[0]
$_[0]
Truth table33%1/3
A || B
A= ref $_[0]B= $_[0]
| |||||||||||||||||||||||||||||
| 39 | 1 | my $self = bless({}, $class); | |||||||||||||||||||||||||||
| 40 | |||||||||||||||||||||||||||||
| 41 | 1 | ▶ | while ( @_ ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 42 | 0 | my ($sub, $value) = (shift, shift); | |||||||||||||||||||||||||||
| 43 | 0 | $self->$sub($value); | |||||||||||||||||||||||||||
| 44 | } | ||||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||
| 46 | 1 | $self->init(); | |||||||||||||||||||||||||||
| 47 | 1 | return $self; | |||||||||||||||||||||||||||
| 48 | 1 | }; | |||||||||||||||||||||||||||
| 49 | } | ||||||||||||||||||||||||||||
| 50 | } | ||||||||||||||||||||||||||||
| 51 | |||||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||
| 53 | 6 | foreach my $sub ( keys %$subs ) { | |||||||||||||||||||||||||||
| 54 | ## construct the method if it is not defined yet. | ||||||||||||||||||||||||||||
| 55 | 13 | ▶ | next if ( defined &{"${pkg}::$sub"} ); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||
| 57 | 13 | ▶ | *{"${pkg}::$sub"} = sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=8 SCAR=20.9
| |||||||||||||||||||||||||||||
| 58 | 25 | my ( $self, $value ) = @_; | |||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||
| 60 | # fill with default at first run | ||||||||||||||||||||||||||||
| 61 | 25 | ▶ | if ( !exists $self->{$sub} ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 62 | 12 | my $val = $subs->{$sub}; | |||||||||||||||||||||||||||
| 63 | 12 | my $rval = ref($val); | |||||||||||||||||||||||||||
| 64 | 12 | ▶ | if (!$rval) { | ||||||||||||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||||||||||||
| 65 | #scalar | ||||||||||||||||||||||||||||
| 66 | 6 | $self->{$sub} = $val; | |||||||||||||||||||||||||||
| 67 | } elsif ($rval =~ /ARRAY/) { | ||||||||||||||||||||||||||||
| 68 | 3 | my @val = @{$val}; | |||||||||||||||||||||||||||
| 69 | 3 | $self->{$sub} = \@val; | |||||||||||||||||||||||||||
| 70 | } elsif($rval =~ /HASH/) { | ||||||||||||||||||||||||||||
| 71 | 3 | my %val = %{$val}; | |||||||||||||||||||||||||||
| 72 | 3 | $self->{$sub} = \%val; | |||||||||||||||||||||||||||
| 73 | } else { | ||||||||||||||||||||||||||||
| 74 | #object ref.. use at own risk. | ||||||||||||||||||||||||||||
| 75 | 0 | $self->{$sub} = $val; | |||||||||||||||||||||||||||
| 76 | } | ||||||||||||||||||||||||||||
| 77 | } | ||||||||||||||||||||||||||||
| 78 | |||||||||||||||||||||||||||||
| 79 | # more then just self, something has to be set. | ||||||||||||||||||||||||||||
| 80 | 25 | ▶ | if ($#_ > 0) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 81 | warn "The value supplied to '$sub()' is not of propper type" | ||||||||||||||||||||||||||||
| 82 | 10 | ▶ | if (ref($subs->{$sub}) and !ref($value)); | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
ref $subs->{$sub} and not ref $value
MC/DC0%0/2
ref $subs->{$sub} and not ref $value
ref $subs->{$sub}
not ref $value
Truth table66%2/3
A and not(B)
A= ref $subs->{$sub}B= not ref $value
| |||||||||||||||||||||||||||||
| 83 | |||||||||||||||||||||||||||||
| 84 | # set the value and return the object. | ||||||||||||||||||||||||||||
| 85 | 10 | $self->{$sub} = $value; | |||||||||||||||||||||||||||
| 86 | 10 | return $self; | |||||||||||||||||||||||||||
| 87 | } | ||||||||||||||||||||||||||||
| 88 | |||||||||||||||||||||||||||||
| 89 | # return the value; | ||||||||||||||||||||||||||||
| 90 | 15 | return $self->{$sub}; | |||||||||||||||||||||||||||
| 91 | |||||||||||||||||||||||||||||
| 92 | 13 | ▶ | } or warn "Method: $sub not implemented\n"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 93 | } | ||||||||||||||||||||||||||||
| 94 | 6 | return 1; # import succeeded | |||||||||||||||||||||||||||
| 95 | } | ||||||||||||||||||||||||||||
| 96 | |||||||||||||||||||||||||||||
| 97 | |||||||||||||||||||||||||||||
| 98 | 4 | 1; | |||||||||||||||||||||||||||
| 99 | |||||||||||||||||||||||||||||
| 100 | __END__ | ||||||||||||||||||||||||||||