blib/lib/constant/lexical.pm

filter: Statementstmt 69.0% 29 / 42 Branchbran 46.4% 13 / 28 Conditioncond 50.0% 4 / 8 MC/DCmcdc 20.0% 1 / 5 Subroutinesub 71.4% 5 / 7 Podpod n/a totaltotal 57.7% 52 / 90 CC 17 SCAR 36.7
CC
17
Cov
59%
_validate
77.3
import
7.0
__ANON__
1.1
CRAP 39.3
↑ Summary lexical.xs »
1 2 ▶use 5.008;
Subroutine BEGIN: called CC=1 SCAR=0.0
2  
3 package constant::lexical;
4  
5 2 our $VERSION = '2.0003'; # Update POD, too!!!!!
6  
7 2 my $old = '#line ' . (__LINE__+1) . " " . __FILE__ . "\n" . <<'__';
8  
9 no constant 1.03 ();
10 use constant hufh => eval 'require Hash::Util::FieldHash';
11 use Sub::Delete;
12 BEGIN {
13 0+$] eq 5.01
14 and VERSION Sub::Delete >= .03
15 and VERSION Sub::Delete 1.00001 # %^H scoping bug
16 }
17 hufh and eval '
18 Hash::Util::FieldHash::fieldhash %hh;
19 use Tie::Hash;
20 {
21 package constant::lexical::_hhfh;
22 @ISA = "Tie::StdHash";
23 sub DELETE { constant::lexical::DESTROY(SUPER::DELETE{@_}) }
24 }
25 tie %hh, constant::lexical::_hhfh::;;
26 ';
27  
28 sub import {
29 $^H |= 0x20000; # magic incantation to make %^H work before 5.10
30 shift;
31 return unless @ '_;
32 my @const = @_ == 1 && ref $_[0] eq 'HASH' ? keys %{$_[0]} : $_[0];
33 my $stashname = caller()."::"; my $stash = \%$stashname;
34 push @{hufh ? $hh{\%^H} ||= [] : ($^H{+__PACKAGE__} ||= bless[])},
35 map {
36 my $fqname = "$stashname$_"; my $ref;
37 if(exists $$stash{$_} && defined $$stash{$_}) {
38 $ref = ref $$stash{$_} eq 'SCALAR'
39 ? $$stash{$_}
40 : *$fqname{CODE};
41 delete_sub($fqname);
42 }
43 [$fqname, $stashname, $_, $ref]
44 } @const;
45 unshift @_, 'constant';
46 goto &{can constant 'import'}
47 }
48  
49 sub DESTROY { for(@{+shift}) {
50 delete_sub(my $fqname = $$_[0]);
51 next unless defined (my $ref = $$_[-1]);
52 ref $ref eq 'SCALAR' or *$fqname = $ref, next;
53 my $stash = \%{$$_[1]}; my $subname = $$_[2];
54 if(exists $$stash{$subname} &&defined $$stash{$subname}) {
55 my $val = $$ref;
56 *$fqname = sub(){$val}
57 } else { $$stash{$subname} = $ref }
58 }}
59  
60 1;
61 __
62  
63 2 my $new = '#line ' . (__LINE__+1) . " " . __FILE__ . "\n" . <<'__';
64  
65 2 ▶BEGIN { $constant::lexical::{lexsubs} = \($] >= 5.022) }
Subroutine BEGIN: called CC=1 SCAR=0.0
66  
67 if (lexsubs) {
68 require XSLoader;
69 XSLoader::load(__PACKAGE__, $VERSION);
70 }
71 else {
72 require Lexical'Sub;
73 }
74  
75 11 ▶sub import {
Subroutine import: called CC=7 SCAR=19.5
76 11 shift;
77 11 ▶ return unless @ '_;
Branch: 1 branch
BranchTrueFalseTotal
unless @_ 1 10 11
78 10 my @args;
79 10 ▶ if(@_ == 1 && ref $_[0] eq 'HASH') {
Branch: 2 branches
BranchTrueFalseTotal
if (@_ == 1 and ref $_[0] eq 'HASH') { } 1 9 10
elsif (@_ == 2) { } 8 1 9
Condition66%2/3
@_ == 1 and ref $_[0] eq "HASH"
!ll&&!rl&&r
901
MC/DC50%1/2
@_ == 1 and ref $_[0] eq "HASH"
@_ == 1 ref $_[0] eq "HASH"
Truth table66%2/3
A and B
A= @_ == 1B= ref $_[0] eq "HASH"
ABresult
0X0
100
111
80 1 _validate(keys %{$_[0]});
81 1 ▶ while(my($k,$v) = each %{$_[0]}) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %{$_[0];}) 2 1 3
82 0 ▶ push @args, $k, sub(){ $v };
Subroutine __ANON__: not called CC=1 SCAR=1.2
83 }
84 }
85 elsif(@_ == 2) {
86 8 _validate($_[0]);
87 8 my $v = pop;
88 0 ▶ @args = ($_[0], sub(){ $v });
Subroutine __ANON__: not called CC=1 SCAR=1.2
89 }
90 else {
91 1 _validate($_[0]);
92 1 ▶ @args = (shift, do { my @v = @'_; sub(){ @v } });
Subroutine __ANON__: called CC=1 SCAR=0.0
93 }
94 10 if (lexsubs) {
95 10 ▶ install_lexical_sub(splice @args, 0, 2) while @args;
Branch: 1 branch
BranchTrueFalseTotal
@args and do { install_lexical_sub(splice(@args, 0, 2)) } 11 10 21
96 }
97 else {
98 import Lexical'Sub @args;
99 }
100 _:
101 }
102  
103 # Plagiarised from constant.pm
104  
105 # Some names are evil choices.
106 my %keywords
107 = map +($_, 1), qw{ BEGIN INIT CHECK END DESTROY AUTOLOAD UNITCHECK };
108  
109 my $normal_constant_name = qr/^_?[^\W_0-9]\w*\z/;
110 my $tolerable = qr/^[A-Za-z_]\w*\z/;
111 my $boolean = qr/^[01]?\z/;
112  
113 10 ▶sub _validate {
Subroutine _validate: called CC=11 SCAR=43.5
114 10 for(@_) {
115 11 ▶ defined or require Carp, Carp'croak("Can't use undef as constant name");
Branch: 1 branch
BranchTrueFalseTotal
defined $_ or require Carp, Carp::croak("Can't use undef as constant name") 11 0 11
116 # Normal constant name
117 11 ▶ if (/$normal_constant_name/ and !$keywords{$_}) {
Branch: 4 branches
BranchTrueFalseTotal
if (/$normal_constant_name/ and not $keywords{$_}) { } 11 0 11
elsif (/^__/) { } 0 0 0
elsif (/$tolerable/) { } 0 0 0
elsif (/$boolean/) { } 0 0 0
Condition33%1/3
/$normal_constant_name/ and not $keywords{$_}
!ll&&!rl&&r
0011
MC/DC0%0/2
/$normal_constant_name/ and not $keywords{$_}
/$normal_constant_name/ not $keywords{$_}
Truth table33%1/3
A and not(B)
A= /$normal_constant_name/B= not $keywords{$_}
ABresult
0X0
100
111
118 # Everything is okay
119  
120 # Starts with double underscore. Fatal.
121 } elsif (/^__/) {
122 0 require Carp;
123 0 Carp::croak("Constant name '$_' begins with '__'");
124  
125 # Maybe the name is tolerable
126 } elsif (/$tolerable/) {
127 # Then we'll warn only if you've asked for warnings
128 0 ▶ if (warnings::enabled()) {
Branch: 1 branch
BranchTrueFalseTotal
if (warnings::enabled()) 0 0 0
129 0 ▶ if ($keywords{$_}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($keywords{$_}) 0 0 0
130 0 warnings::warn("Constant name '$_' is a Perl keyword");
131 }
132 }
133  
134 # Looks like a boolean
135 # use constant FRED == fred;
136 } elsif (/$boolean/) {
137 0 require Carp;
138 0 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) { } 0 0 0
139 0 Carp::croak("Constant name '$_' is invalid");
140 } else {
141 0 Carp::croak("Constant name looks like boolean value");
142 }
143  
144 } else {
145 # Must have bad characters
146 0 require Carp;
147 0 Carp::croak("Constant name '$_' has invalid characters");
148 }
149 }
150 }
151  
152 1;
153 __
154  
155 2 ▶eval($] < 5.011002 ? $old : $new) or die $@;
Branch: 1 branch
BranchTrueFalseTotal
$] < 5.011002 ? : 0 2 2
Condition50%1/2
eval($] < 5.011002 ? $old : $new) || die($@)
l!l
20
MC/DC0%0/1
eval($] < 5.011002 ? $old : $new) || die($@)
eval($] < 5.011002 ? $old : $new)
Truth table50%1/2
A
A= eval($] < 5.011002 ? $old : $new)
Aresult
11
00
156  
157 __END__
↑ Summary lexical.xs »