| 1 | package Class::Constant; | ||||||||||||||||||||||||||||
| 2 | 8 | $Class::Constant::VERSION = '0.07'; | |||||||||||||||||||||||||||
| 3 | # ABSTRACT: Build constant classes | ||||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||
| 5 | 8 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | 8 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||
| 8 | 8 | ▶ | use Scalar::Util qw(looks_like_number); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||
| 10 | 8 | my %ordinal_for_data; | |||||||||||||||||||||||||||
| 11 | my %data_by_ordinal; | ||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||
| 13 | 10 | ▶ | sub import { | ||||||||||||||||||||||||||
|
Subroutine
import: called CC=13 SCAR=25.7
| |||||||||||||||||||||||||||||
| 14 | 10 | my ($pkg, @args) = @_; | |||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||
| 16 | 10 | my $caller = caller; | |||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||
| 18 | 10 | ▶ | $ordinal_for_data{$caller} ||= 0; | ||||||||||||||||||||||||||
|
Condition100%2/2
$ordinal_for_data{$caller} ||= 0
MC/DC100%1/1
$ordinal_for_data{$caller} ||= 0
$ordinal_for_data{$caller}
Truth table100%2/2
A
A= $ordinal_for_data{$caller}
| |||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||
| 20 | 10 | my $start_ordinal = $ordinal_for_data{$caller}; | |||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||
| 22 | 10 | my %data; | |||||||||||||||||||||||||||
| 23 | 10 | my $value = 0; | |||||||||||||||||||||||||||
| 24 | 10 | for my $arg (@args) { | |||||||||||||||||||||||||||
| 25 | 57 | ▶ | if ($arg =~ /^[A-Z][A-Z0-9_]*$/) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 26 | 35 | ▶ | if (exists $data{name}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 27 | 26 | my %data_copy = %data; | |||||||||||||||||||||||||||
| 28 | 26 | $data_by_ordinal{$caller}->[$data{ordinal}] = \%data_copy; | |||||||||||||||||||||||||||
| 29 | } | ||||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||
| 31 | 35 | %data = (); | |||||||||||||||||||||||||||
| 32 | |||||||||||||||||||||||||||||
| 33 | 35 | $data{name} = $arg; | |||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||
| 35 | 35 | $data{ordinal} = $ordinal_for_data{$caller}; | |||||||||||||||||||||||||||
| 36 | 35 | $ordinal_for_data{$caller}++; | |||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||||
| 38 | 35 | $data{object} = \do { my $x = $data{ordinal} }; | |||||||||||||||||||||||||||
| 39 | |||||||||||||||||||||||||||||
| 40 | 35 | $data{value} = $value; | |||||||||||||||||||||||||||
| 41 | 35 | ▶ | $value++ if looks_like_number($value); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 42 | |||||||||||||||||||||||||||||
| 43 | 35 | next; | |||||||||||||||||||||||||||
| 44 | } | ||||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||
| 46 | 22 | ▶ | if (ref $arg eq "HASH") { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 47 | 8 | $data{methods} = $value = $arg; | |||||||||||||||||||||||||||
| 48 | 8 | ▶ | $value++ if looks_like_number($value); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||
| 50 | 8 | next; | |||||||||||||||||||||||||||
| 51 | } | ||||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||
| 53 | 14 | $data{value} = $value = $arg; | |||||||||||||||||||||||||||
| 54 | 14 | ▶ | $value++ if looks_like_number($value); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 55 | } | ||||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||
| 57 | 10 | ▶ | if (exists $data{name}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 58 | 9 | my %data_copy = %data; | |||||||||||||||||||||||||||
| 59 | 9 | $data_by_ordinal{$caller}->[$data{ordinal}] = \%data_copy; | |||||||||||||||||||||||||||
| 60 | } | ||||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||
| 62 | 10 | for my $ordinal ($start_ordinal .. $ordinal_for_data{$caller}-1) { | |||||||||||||||||||||||||||
| 63 | 35 | my $data = $data_by_ordinal{$caller}->[$ordinal]; | |||||||||||||||||||||||||||
| 64 | |||||||||||||||||||||||||||||
| 65 | 35 | do { | |||||||||||||||||||||||||||
| 66 | 8 | ▶ | no strict "refs"; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 67 | 35 | ▶ | *{$caller."::".$data->{name}} = sub { bless $data->{object}, $caller }; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 68 | }; | ||||||||||||||||||||||||||||
| 69 | } | ||||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||
| 71 | 10 | ▶ | if ($start_ordinal == 0 and $ordinal_for_data{$caller} > 0) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$start_ordinal == 0 and $ordinal_for_data{$caller} > 0
MC/DC100%2/2
$start_ordinal == 0 and $ordinal_for_data{$caller} > 0
$start_ordinal == 0
$ordinal_for_data{$caller} > 0
Truth table100%3/3
A and B
A= $start_ordinal == 0B= $ordinal_for_data{$caller} > 0
| |||||||||||||||||||||||||||||
| 72 | 8 | do { | |||||||||||||||||||||||||||
| 73 | 8 | ▶ | no strict "refs"; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 74 | |||||||||||||||||||||||||||||
| 75 | 8 | unshift @{$caller."::ISA"}, "Class::Constant::Object"; | |||||||||||||||||||||||||||
| 76 | |||||||||||||||||||||||||||||
| 77 | 8 | ▶ | *{$caller."::by_ordinal"} = sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=4 SCAR=14.3
| |||||||||||||||||||||||||||||
| 78 | 7 | ▶ | return if @_ < 2; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 79 | 7 | ▶ | if (not exists $data_by_ordinal{$caller}->[$_[1]]) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 80 | 1 | require Carp; | |||||||||||||||||||||||||||
| 81 | 1 | ▶ | Carp::croak("Can't locate constant with ordinal \"$_[1]\" in package \"".(ref($_[0])||$_[0])."\""); | ||||||||||||||||||||||||||
|
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]
| |||||||||||||||||||||||||||||
| 82 | } | ||||||||||||||||||||||||||||
| 83 | 6 | return bless $data_by_ordinal{$caller}->[$_[1]]->{object}, $caller; | |||||||||||||||||||||||||||
| 84 | 8 | }; | |||||||||||||||||||||||||||
| 85 | }; | ||||||||||||||||||||||||||||
| 86 | } | ||||||||||||||||||||||||||||
| 87 | } | ||||||||||||||||||||||||||||
| 88 | |||||||||||||||||||||||||||||
| 89 | |||||||||||||||||||||||||||||
| 90 | package | ||||||||||||||||||||||||||||
| 91 | Class::Constant::Object; | ||||||||||||||||||||||||||||
| 92 | |||||||||||||||||||||||||||||
| 93 | 8 | ▶ | use Scalar::Util qw(refaddr blessed); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 94 | |||||||||||||||||||||||||||||
| 95 | use overload | ||||||||||||||||||||||||||||
| 96 | 22 | ▶ | q{""} => sub { (shift)->as_string(@_) }, | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 97 | 9 | ▶ | q{==} => sub { !!((shift)->equals(@_)) }, | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 98 | 6 | ▶ | q{!=} => sub { !((shift)->equals(@_)) }, | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 99 | 22 | ▶ | q{eq} => sub { !!((shift)->equals(@_)) }, | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 100 | 0 | ▶ | q{ne} => sub { !((shift)->equals(@_)) }; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=0.2
| |||||||||||||||||||||||||||||
| 101 | |||||||||||||||||||||||||||||
| 102 | 21 | ▶ | sub as_string { | ||||||||||||||||||||||||||
|
Subroutine
as_string: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 103 | 21 | return "$data_by_ordinal{ref $_[0]}->[${$_[0]}]->{value}"; | |||||||||||||||||||||||||||
| 104 | } | ||||||||||||||||||||||||||||
| 105 | |||||||||||||||||||||||||||||
| 106 | 37 | ▶ | sub equals { | ||||||||||||||||||||||||||
|
Subroutine
equals: called CC=4 SCAR=13.9
| |||||||||||||||||||||||||||||
| 107 | 37 | ▶ | if (blessed $_[1] and $_[1]->isa(__PACKAGE__)) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
builtin::blessed($_[1]) and $_[1]->isa(__PACKAGE__)
MC/DC50%1/2
builtin::blessed($_[1]) and $_[1]->isa(__PACKAGE__)
builtin::blessed($_[1])
$_[1]->isa(__PACKAGE__)
Truth table66%2/3
A and B
A= builtin::blessed($_[1])B= $_[1]->isa(__PACKAGE__)
| |||||||||||||||||||||||||||||
| 108 | 15 | ▶ | return (refaddr $_[0] == refaddr $_[1]) ? 1 : 0; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 109 | } | ||||||||||||||||||||||||||||
| 110 | |||||||||||||||||||||||||||||
| 111 | 22 | return "".$_[0] eq "".$_[1]; | |||||||||||||||||||||||||||
| 112 | } | ||||||||||||||||||||||||||||
| 113 | |||||||||||||||||||||||||||||
| 114 | 6 | ▶ | sub get_ordinal { | ||||||||||||||||||||||||||
|
Subroutine
get_ordinal: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 115 | 6 | return ${$_[0]}; | |||||||||||||||||||||||||||
| 116 | } | ||||||||||||||||||||||||||||
| 117 | |||||||||||||||||||||||||||||
| 118 | 16 | ▶ | sub AUTOLOAD { | ||||||||||||||||||||||||||
|
Subroutine
AUTOLOAD: called CC=6 SCAR=20.3
| |||||||||||||||||||||||||||||
| 119 | 16 | my ($self) = @_; | |||||||||||||||||||||||||||
| 120 | |||||||||||||||||||||||||||||
| 121 | 8 | ▶ | use vars qw($AUTOLOAD); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 122 | 16 | my ($pkg, $method) = $AUTOLOAD =~ m/^(.*)::(.*)/; | |||||||||||||||||||||||||||
| 123 | |||||||||||||||||||||||||||||
| 124 | 16 | ▶ | return if $method =~ m/^[A-Z]+$/; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 125 | |||||||||||||||||||||||||||||
| 126 | 16 | ▶ | if ($method !~ m/^get_/) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 127 | 0 | require Carp; | |||||||||||||||||||||||||||
| 128 | 0 | Carp::croak("Can't locate object method \"$method\" via package \"$pkg\""); | |||||||||||||||||||||||||||
| 129 | } | ||||||||||||||||||||||||||||
| 130 | |||||||||||||||||||||||||||||
| 131 | 16 | my ($name) = $method =~ m/^get_(.*)/; | |||||||||||||||||||||||||||
| 132 | |||||||||||||||||||||||||||||
| 133 | 16 | my $data = $data_by_ordinal{ref $_[0]}->[${$_[0]}]; | |||||||||||||||||||||||||||
| 134 | 16 | ▶ | return if not $data; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 135 | |||||||||||||||||||||||||||||
| 136 | 16 | ▶ | if (not exists $data->{methods} or not exists $data->{methods}->{$name}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
exists $data->{"methods"} and exists $data->{"methods"}{$name}
MC/DC0%0/2
exists $data->{"methods"} and exists $data->{"methods"}{$name}
exists $data->{"methods"}
exists $data->{"methods"}{$name}
Truth table33%1/3
A and B
A= exists $data->{"methods"}B= exists $data->{"methods"}{$name}
| |||||||||||||||||||||||||||||
| 137 | 0 | require Carp; | |||||||||||||||||||||||||||
| 138 | 0 | Carp::croak("Can't locate named constant \"$name\" for \"" .ref($_[0]). "::$data->{name}\""); | |||||||||||||||||||||||||||
| 139 | } | ||||||||||||||||||||||||||||
| 140 | |||||||||||||||||||||||||||||
| 141 | 16 | return $data->{methods}->{$name}; | |||||||||||||||||||||||||||
| 142 | } | ||||||||||||||||||||||||||||
| 143 | |||||||||||||||||||||||||||||
| 144 | 8 | 1; | |||||||||||||||||||||||||||
| 145 | |||||||||||||||||||||||||||||
| 146 | __END__ | ||||||||||||||||||||||||||||