blib/lib/Lexical/Attributes.pm

filter: Statementstmt 96.4% 81 / 84 Branchbran 91.6% 33 / 36 Conditioncond 83.3% 5 / 6 MC/DCmcdc 50.0% 2 / 4 Subroutinesub 100% 17 / 17 Podpod 0.0% 0 / 4 totaltotal 91.3% 138 / 151 CC 22 SCAR 17.4
CC
22
Cov
94%
declare_attribute
13.0
use_attribute
3.1
__ANON__
3.0
CRAP 5.7
↑ Summary
1 package Lexical::Attributes;
2  
3 10 ▶use 5.008001;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 10 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 10 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 10 ▶no warnings 'syntax';
Subroutine BEGIN: called CC=1 SCAR=0.0
8 10 ▶use Filter::Simple;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 10 ▶use Scalar::Util;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 10 our $VERSION = '2009121601';
12  
13 10 my $sigil = '[$@%]';
14 10 my $sec_sigil = '[.]';
15 10 my $trait = '(?:r[ow]|pr(?:iv)?)'; # read-only, read-write, private.
16 10 my $name = qr /[a-zA-Z_][a-zA-Z0-9_]*/; # Starts with alpha or _, followed
17 # by one or more alphanumunders.
18 10 my $has_attribute = qr /(?>$sigil$sec_sigil$name)/;
19 10 my $use_attribute = qr /(?>(?:\$#?|[%\@])$sec_sigil$name)/;
20 10 my $int_attribute = qr /(?>(?:\$#?|\@)$sec_sigil$name)/; # No hash.
21  
22 10 my %attributes;
23  
24 52 ▶sub declare_attribute {
Subroutine declare_attribute: called CC=13 SCAR=25.7
Pod undocumented
25 52 my ($attributes, $trait) = @_;
26  
27 52 ▶ $trait = "pr" if !$trait || $trait eq "priv";
Branch: 1 branch
BranchTrueFalseTotal
if not $trait or $trait eq "priv" 16 36 52
Condition100%3/3
not $trait or $trait eq "priv"
l!l&&r!l&&!r
13336
MC/DC100%2/2
not $trait or $trait eq "priv"
not $trait $trait eq "priv"
Truth table100%3/3
not(A) or B
A= not $traitB= $trait eq "priv"
ABresult
1X1
011
000
28  
29 52 my $text = "";
30  
31 52 foreach my $attribute (split /\s*,\s*/ => $attributes) {
32  
33 64 my ($sigil, $sec_sigil, $name) = unpack "A A A*" => $attribute;
34 64 my $str = "";
35  
36 64 ▶ if ($attributes {$name}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($attributes{$name}) 1 63 64
37 1 warn "Duplicate attribute '$attribute' ignored\n";
38 1 next;
39 }
40  
41 63 $attributes {$name} = [$sigil, $trait];
42  
43 63 $str .= "my %$name;";
44 63 ▶ unless ($trait eq "pr") {
Branch: 1 branch
BranchTrueFalseTotal
unless ($trait eq "pr") 41 22 63
45 41 ▶ if ($sigil eq '$') {
Branch: 3 branches
BranchTrueFalseTotal
if ($sigil eq '$') { } 27 14 41
elsif ($sigil eq '@') { } 7 7 14
elsif ($sigil eq '%') { } 7 0 7
46 27 $str .= <<' --';
47 sub _NAME {
48 my $_key = Scalar::Util::refaddr shift;
49 $_NAME {$_key}
50 }
51 --
52 }
53 # @_ ? @{$_NAME {$_key}} [@_] : @{$_NAME {$_key}};
54 elsif ($sigil eq '@') {
55 7 $str .= <<' --';
56 sub _NAME {
57 my $_key = Scalar::Util::refaddr shift;
58 @_ ? @{$_NAME {$_key}} [@_] : @{$_NAME {$_key} || []};
59 }
60 --
61 }
62 elsif ($sigil eq '%') {
63 7 $str .= <<' --';
64 sub _NAME {
65 my $_key = Scalar::Util::refaddr shift;
66 @_ ? @{$_NAME {$_key}} {@_} :
67 wantarray ? %{$_NAME {$_key} || {}} :
68 keys %{$_NAME {$_key} || {}};
69 }
70 --
71 }
72 else {
73 0 die "'$attribute' not implemented\n";
74 }
75  
76 41 ▶ if ($trait eq "rw") {
Branch: 1 branch
BranchTrueFalseTotal
if ($trait eq "rw") 29 12 41
77 29 ▶ if ($sigil eq '$') {
Branch: 3 branches
BranchTrueFalseTotal
if ($sigil eq '$') { } 17 12 29
elsif ($sigil eq '@') { } 6 6 12
elsif ($sigil eq '%') { } 6 0 6
78 17 $str .= <<' --';
79 sub set__NAME {
80 my $self = shift;
81 my $_key = Scalar::Util::refaddr $self;
82 $_NAME {$_key} = shift;
83 $self;
84 }
85 --
86 }
87 elsif ($sigil eq '@') {
88 6 $str .= <<' --';
89 sub set__NAME {
90 my $self = shift;
91 my $_key = Scalar::Util::refaddr $self;
92 if (@_ == 0) {delete $_NAME {$_key}}
93 elsif (@_ == 1) {
94 if (ref $_ [0] eq 'ARRAY') {$_NAME {$_key} = $_ [0]}
95 else {delete $_NAME {$_key} [$_ [0]]}
96 }
97 else {
98 while (@_ >= 2) {
99 my ($index, $value) = splice @_ => 0, 2;
100 $_NAME {$_key} [$index] = $value;
101 }
102 }
103 $self;
104 }
105 --
106 }
107 elsif ($sigil eq '%') {
108 6 $str .= <<' --';
109 sub set__NAME {
110 my $self = shift;
111 my $_key = Scalar::Util::refaddr $self;
112 if (@_ == 0) {delete $_NAME {$_key}}
113 elsif (@_ == 1) {
114 if (ref $_ [0] eq 'HASH') {$_NAME {$_key} = $_ [0]}
115 else {delete $_NAME {$_key} {$_ [0]}}
116 }
117 else {
118 while (@_ >= 2) {
119 my ($key, $value) = splice @_ => 0, 2;
120 $_NAME {$_key} {$key} = $value;
121 }
122 }
123 $self;
124 }
125 --
126 }
127 }
128 }
129 63 $str =~ s/\n\s*/ /g;
130 63 $str =~ s/_NAME/$name/g;
131  
132 63 $text .= $str;
133 }
134
135 52 return $text;
136 }
137  
138 13 ▶sub destroy_attributes {
Subroutine destroy_attributes: called CC=2 SCAR=6.9
Pod undocumented
139 13 my $str = "";
140 13 ▶ while (my ($key) = each %attributes) {
Branch: 1 branch
BranchTrueFalseTotal
if (my($key) = each %attributes) 63 13 76
141 63 $str .= "delete \$$key {Scalar::Util::refaddr \$self};\n";
142 }
143 13 $str;
144 }
145  
146 95 ▶sub use_attribute {
Subroutine use_attribute: called CC=3 SCAR=11.3
Pod undocumented
147 95 my ($attribute) = @_;
148  
149 95 my ($sigil, $name) = split /[.]/ => $attribute, 2;
150  
151 95 ▶ if (!$attributes {$name}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($attributes{$name}) 0 95 95
152 0 die $_;
153 0 die qq !Attribute "$attribute" requires declaration!;
154 }
155  
156 95 my $str;
157 95 ▶ if ($sigil eq '$') {
Branch: 1 branch
BranchTrueFalseTotal
if ($sigil eq '$') { } 62 33 95
158 62 $str = "\$$name\{Scalar::Util::refaddr \$self}";
159 }
160 else {
161 33 $str = "$sigil\{\$$name\{Scalar::Util::refaddr \$self}}";
162 }
163 95 $str;
164 }
165  
166 24 ▶sub interpolate {
Subroutine interpolate: called CC=2 SCAR=6.9
Pod undocumented
167 24 local $_ = shift;
168  
169 # The regex below finds attribute names. We cannot simply use a
170 # regex for finding them, we need to parse the entire string, to
171 # be able to deal with backslashes.
172 #
173 # We use loop unrolling for efficiency.
174 #
175 24 s {( # Capture non attributes in $1.
176 [^\$\@\\]* # Anything that isn't $, @ or \ is ok.
177 (?: # Group (1)
178 (?: # Group (2) ("Special things")
179 \\. # Escape followed by any character
180 | # or
181 (?: # Group (3)
182 (?:>\$\#?) # Scalar sigil, or array count
183 # The (?> ) is vital here.
184 | # Or
185 \@ # Array sigil
186 ) # End group (3)
187 (?!\.$name) # not followed by dot attribute name
188 ) # End group (2)
189 [^\$\@\\]* # Anything that isn't $, @ or \
190 )* # End group (1), repeated zero or more times.
191 ) # End capture $1
192 | # Or
193 ( # In $2, capture an attribute
194 (?:(?>\$\#?)|\@) # Primary sigil
195 \.$name # dot attribute name
196 ) # End $2
197 }
198 122 ▶ {defined $1 ? $1 : use_attribute ($2)}sexg;
Branch: 1 branch
BranchTrueFalseTotal
defined $1 ? : 100 22 122
199  
200 24 $_
201 }
202  
203 FILTER_ONLY
204 #
205 # Initialize variables.
206 #
207 13 ▶ all => sub {%attributes = ()},
Subroutine __ANON__: called CC=1 SCAR=0.0
208  
209 #
210 # Save all attributes found in comments. *Very* simple heuristics
211 # to determine comments - note that quote like constructs have been
212 # moved out of the way.
213 #
214 # Moving away the attributes found in comments prevents subsequent
215 # passes to modify them. In particular, outcommented attribute
216 # declarations shouldn't create methods or hashes.
217 #
218 13 ▶ code => sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
219 13 ▶ 1 while s/( # Save
Branch: 1 branch
BranchTrueFalseTotal
s/( # Save (?<!$sigil) # Not preceeded by a sigil \# # Start of a comment. [^\n]* # Not a newline, not an attribute, (?: $sigil (?!$sec_sigil) [^\n]*)* # using standard unrolling. ) ($sigil) ($sec_sigil) ($name) /$1$2<$3>$4/gx and do { 1 } 1 13 14
220 (?<!$sigil) # Not preceeded by a sigil
221 \# # Start of a comment.
222 [^\n]* # Not a newline, not an attribute,
223 (?: $sigil (?!$sec_sigil) [^\n]*)*
224 # using standard unrolling.
225 ) ($sigil) ($sec_sigil) ($name)
226 /$1$2<$3>$4/xg;
227 },
228  
229 #
230 # Find the attribute declarions and uses. Foreach declararion, the sub
231 # 'attribute' is called, which will create an attribute hash,
232 # and, for non-private attributes, a constructor (which maybe
233 # an lvalue method if the rw trait is given).
234 #
235 # We recognize:
236 # "has" [$@%].attribute ( ("is")? "pr(iv)?|ro|rw")? ";"
237 # "has" "(" [$@%].attribute ("," [$@%].attribute)* ")" \
238 # ( ("is")? "pr(iv)?|ro|rw")? ";"
239 #
240 # Other attribute usages are just:
241 # ([$@%]|$#).attribute
242 #
243 # Attribute uses are handled by calling 'use_attribute'.
244 #
245 13 ▶ code => sub {
Subroutine __ANON__: called CC=3 SCAR=11.1
246 13 s{(?: # Declaration using 'has',
247 \bhas \s* # Must start with "has"
248 (?: # Either
249 ($has_attribute) # a single ttribute, stored in $1.
250 | # or
251 [(] \s* ($has_attribute (?: \s* , \s* $has_attribute)*) \s* [)]
252 # an attribute list, stored in $2.
253 )
254 (?: \s* (?:is \s+)? ($trait))? # Optional trait - stored in $3.
255 \s* ; # Terminated by semi-colon.
256 ) | # or actual usage.
257 ($use_attribute) # It's in $4.
258 }
259 125 ▶ {$4 ? use_attribute ($4) : declare_attribute ($1 || $2, $3)}egx;
Branch: 1 branch
BranchTrueFalseTotal
$4 ? : 73 52 125
Condition66%2/3
$1 || $2
l!l&&r!l&&!r
41110
MC/DC0%0/2
$1 || $2
$1 $2
Truth table66%2/3
A || B
A= $1B= $2
ABresult
1X1
011
000
260 },
261  
262 #
263 # Interpolation. Double quoted strings, backticks, slashes and q[qrx] {},
264 # m// and s/// constructs.
265 #
266 # Note that '', qw{}, tr///, m'' and s''' don't interpolate.
267 #
268 # How to test qx?
269 #
270 48 ▶ quotelike => sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
271 48 ▶ if (m {^(["`/]|q[qrx]\s*\S|[sm]\s*[^\s'])(.*)(\S)$}s) {
Branch: 1 branch
BranchTrueFalseTotal
if (m[^(["`/]|q[qrx]\s*\S|[sm]\s*[^\s'])(.*)(\S)$]s) 24 24 48
272 24 $_ = $1 . interpolate ($2) . $3
273 }
274 },
275  
276 #
277 # If a subroutine uses the keyword 'method' (at the beginning of
278 # a line), add an assignment to '$self'.
279 #
280  
281 13 ▶ code => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
282 13 s<^(\s*) method (\s+ [a-zA-Z_]\w* \s* # sub name
283 (?:\([^)]*\) \s*)? # Optional prototype
284 \{) # Opening of block
285 ><$1 sub $2 my \$self = shift;>mgx;
286 },
287  
288 #
289 # Add a DESTROY function
290 #
291 13 ▶ code => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
292 13 my $destroy = <<' --';
293  
294 sub DESTROY {
295 our @ISA;
296 my $self = shift;
297 my $DESTRUCT = __PACKAGE__ . "::DESTRUCT";
298 $self -> $DESTRUCT if do {no strict 'refs'; exists &$DESTRUCT};
299 DESTROY_ATTRIBUTES;
300 foreach my $class (@ISA) {
301 my $destroy = $class . "::DESTROY";
302 $self -> $destroy if $self -> can ($destroy);
303 }
304 }
305 --
306  
307 13 my $destroy_attributes = destroy_attributes;
308  
309 13 $destroy =~ s/DESTROY_ATTRIBUTES/$destroy_attributes/;
310 13 $destroy =~ s/^ {8}//gm;
311  
312 13 $_ .= $destroy;
313 },
314  
315 #
316 # Restore tucked away, outcommented, attributes.
317 #
318 13 ▶ code => sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
319 13 ▶ 1 while s/( # Save
Branch: 1 branch
BranchTrueFalseTotal
s/( # Save (?<!$sigil) # Not preceeded by a sigil \# # Start of a comment. [^\n]* # Not a newline, not an attribute, (?: $sigil (?!<$sec_sigil>) [^\n]*)* # using standard unrolling. ) ($sigil) <($sec_sigil)> ($name) /$1$2$3$4/gx and do { 1 } 1 13 14
320 (?<!$sigil) # Not preceeded by a sigil
321 \# # Start of a comment.
322 [^\n]* # Not a newline, not an attribute,
323 (?: $sigil (?!<$sec_sigil>) [^\n]*)*
324 # using standard unrolling.
325 ) ($sigil) <($sec_sigil)> ($name)
326 /$1$2$3$4/xg;
327 },
328  
329 #
330 # For debugging purposes; to be removed.
331 #
332 # all => sub {print "<<$_>>\n" if $::DEBUG || $ENV {DEBUG}},
333  
334 10 ;
335  
336 __END__
↑ Summary