GX/Meta/Package.pm

filter: Statementstmt 84.0% 74 / 88 Branchbran 57.1% 16 / 28 Conditioncond 80.0% 4 / 5 MC/DCmcdc 66.6% 2 / 3 Subroutinesub 85.7% 18 / 21 Podpod 100% 8 / 8 totaltotal 79.7% 122 / 153 CC 18 SCAR 15.9
CC
18
Cov
78%
clear_typeglob_slot
11.6
assign_to_typeglob
5.5
new
4.5
CRAP 4.9
« GX/Meta/Attribute/String.pm ↑ Summary GX/Meta/Attribute/Array.pm »
1 # ----------------------------------------------------------------------------------------------------------------------
2 # GX Framework (c) 2009-2011 Jörg A. Uzarek <uzarek@runlevelnull.de>
3 # File: GX/Meta/Package.pm
4 # ----------------------------------------------------------------------------------------------------------------------
5  
6 package GX::Meta::Package;
7  
8 274 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 274 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 274 ▶use GX::Meta::Constants qw( REGEX_PACKAGE_NAME REGEX_SYMBOL_NAME );
Subroutine BEGIN: called CC=1 SCAR=0.0
12 274 ▶use GX::Meta::Exception;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 274 ▶use GX::Meta::Util;
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15  
16 # ----------------------------------------------------------------------------------------------------------------------
17 # Overloading
18 # ----------------------------------------------------------------------------------------------------------------------
19  
20 use overload
21 0 ▶ 'bool' => sub { 1 },
Subroutine BEGIN: called CC=1 SCAR=0.4
Subroutine __ANON__: not called CC=1 SCAR=6.9
22 0 ▶ '0+' => sub { $_[0] },
Subroutine __ANON__: not called CC=1 SCAR=6.9
23 1 ▶ '""' => sub { $_[0]->name },
Subroutine __ANON__: called CC=1 SCAR=0.0
24 274 'fallback' => 1;
25  
26  
27 # ----------------------------------------------------------------------------------------------------------------------
28 # Constructor
29 # ----------------------------------------------------------------------------------------------------------------------
30  
31 1221 ▶sub new {
Subroutine new: called CC=4 SCAR=15.0
Pod documented
32  
33 1221 my $class = shift;
34 1221 ▶ my %args = ( @_ == 1 ) ? ( 'name' => $_[0] ) : @_;
Branch: 1 branch
BranchTrueFalseTotal
@_ == 1 ? : 1220 1 1221
35  
36 1221 ▶ if ( ! defined $args{'name'} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $args{"name"}) 0 1221 1221
37 0 complain "Missing argument (\"name\")";
38 }
39  
40 1221 ▶ if ( $args{'name'} !~ REGEX_PACKAGE_NAME ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($args{"name"} =~ /^[a-zA-Z_]\w*(?:::[a-zA-Z_]\w*)*$/) 0 1221 1221
41 0 complain "Invalid argument (\"$args{'name'}\" is not a valid package name)";
42 }
43  
44 return bless {
45 'name' => $args{'name'},
46 1221 'prefix' => $args{'name'} . '::'
47 }, $class;
48  
49 }
50  
51  
52 # ----------------------------------------------------------------------------------------------------------------------
53 # Public methods
54 # ----------------------------------------------------------------------------------------------------------------------
55  
56 13191 ▶sub assign_to_typeglob {
Subroutine assign_to_typeglob: called CC=5 SCAR=17.1
Pod documented
57  
58 13191 my $self = shift;
59 13191 my $symbol_name = shift;
60 13191 my $reference = shift;
61  
62 13191 ▶ if ( @_ ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 0 13191 13191
63 0 complain "Invalid number of arguments";
64 }
65  
66 13191 ▶ if ( ! defined $symbol_name ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $symbol_name) 0 13191 13191
67 0 complain "Invalid argument (undefined symbol name)";
68 }
69  
70 13191 ▶ if ( $symbol_name !~ REGEX_SYMBOL_NAME ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($symbol_name =~ /^[a-zA-Z_]\w*(?:::[a-zA-Z_]\w*)*$/) 0 13191 13191
71 0 complain "Invalid argument (\"$symbol_name\" is not a valid symbol name)";
72 }
73  
74 13191 ▶ if ( ref $reference !~ /^(?:ARRAY|CODE|HASH|SCALAR)$/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref !($reference =~ /^(?:ARRAY|CODE|HASH|SCALAR)$/)) 0 13191 13191
75 0 complain "Invalid argument (value must be a scalar, array, hash or code reference)";
76 }
77  
78 {
79 274 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
80 274 ▶ no warnings 'redefine';
Subroutine BEGIN: called CC=1 SCAR=0.0
81 13191 *{ $self->qualify( $symbol_name ) } = $reference;
82 }
83  
84 13191 return;
85  
86 }
87  
88 5233 ▶sub clear_typeglob_slot {
Subroutine clear_typeglob_slot: called CC=10 SCAR=24.5
Pod documented
89  
90 5233 my $self = shift;
91 5233 my $symbol_name = shift;
92 5233 my $slot = shift;
93  
94 5233 ▶ if ( @_ ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 0 5233 5233
95 0 complain "Invalid number of arguments";
96 }
97  
98 5233 ▶ if ( ! defined $symbol_name ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $symbol_name) 0 5233 5233
99 0 complain "Invalid argument (undefined symbol name)";
100 }
101  
102 5233 ▶ if ( $symbol_name !~ REGEX_SYMBOL_NAME ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($symbol_name =~ /^[a-zA-Z_]\w*(?:::[a-zA-Z_]\w*)*$/) 0 5233 5233
103 0 complain "Invalid argument (\"$symbol_name\" is not a valid symbol name)";
104 }
105  
106 5233 ▶ if ( ! defined $slot ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $slot) 0 5233 5233
107 0 complain "Invalid argument (undefined typeglob slot)";
108 }
109  
110 5233 ▶ if ( $slot !~ /^(?:SCALAR|ARRAY|HASH|CODE|IO)$/ ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($slot =~ /^(?:SCALAR|ARRAY|HASH|CODE|IO)$/) 0 5233 5233
111 0 complain "Invalid argument (\"$slot\" is not a valid typeglob slot)";
112 }
113  
114 5233 ▶ return unless exists $self->symbol_table->{$symbol_name};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $self->symbol_table->{$symbol_name} 0 5233 5233
115  
116 {
117  
118 5233 my $qualified_symbol_name = $self->qualify( $symbol_name );
119  
120 274 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
121  
122 5233 local *old_typeglob = *{$qualified_symbol_name};
123 5233 local *new_typeglob;
124  
125 5233 for ( qw( SCALAR ARRAY HASH CODE IO ) ) {
126 26165 ▶ *new_typeglob = *old_typeglob{$_} if defined( *old_typeglob{$_} ) && $_ ne $slot;
Branch: 1 branch
BranchTrueFalseTotal
if defined *old_typeglob{$_} and $_ ne $slot 5240 20925 26165
Condition100%3/3
defined *old_typeglob{$_} and $_ ne $slot
!ll&&!rl&&r
1569252335240
MC/DC100%2/2
defined *old_typeglob{$_} and $_ ne $slot
defined *old_typeglob{$_} $_ ne $slot
Truth table100%3/3
A and B
A= defined *old_typeglob{$_}B= $_ ne $slot
ABresult
0X0
100
111
127 }
128  
129 5233 *{$qualified_symbol_name} = *new_typeglob;
130  
131 }
132  
133 5233 return 1;
134  
135 }
136  
137 4 ▶sub name {
Subroutine name: called CC=1 SCAR=0.0
Pod documented
138  
139 4 return $_[0]->{'name'};
140  
141 }
142  
143 0 ▶sub prefix {
Subroutine prefix: not called CC=1 SCAR=6.9
Pod documented
144  
145 0 return $_[0]->{'prefix'};
146  
147 }
148  
149 18424 ▶sub qualify {
Subroutine qualify: called CC=2 SCAR=7.6
Pod documented
150  
151 18424 ▶ return $_[0]->{'prefix'} . ( $_[1] // '' );
Condition50%1/2
$_[1] // ""
l!l
184240
MC/DC0%0/1
$_[1] // ""
$_[1]
Truth table50%1/2
A
A= $_[1]
Aresult
11
00
152  
153 }
154  
155 16690 ▶sub symbol_table {
Subroutine symbol_table: called CC=1 SCAR=0.0
Pod documented
156  
157 274 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
158  
159 16690 return \%{$_[0]->{'prefix'}};
160  
161 }
162  
163 2 ▶sub wipe {
Subroutine wipe: called CC=1 SCAR=0.0
Pod documented
164  
165 2 my $self = shift;
166  
167 2 GX::Meta::Util::wipe_package( $self->{'name'} );
168  
169 2 return;
170  
171 }
172  
173  
174 274 1;
175  
176 __END__
« GX/Meta/Attribute/String.pm ↑ Summary GX/Meta/Attribute/Array.pm »