Coro/Localize.pm

filter: Statementstmt 75.6% 84 / 111 Branchbran 47.0% 16 / 34 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 88.8% 16 / 18 Podpod 0.0% 0 / 13 totaltotal 65.9% 116 / 176 CC 22 SCAR 17.5
CC
22
Cov
69%
croak
20.0
parser
8.0
strip_close_paren
6.0
CRAP 5.7
↑ Summary Syntax/Feature/Corolocal.pm »
1 # ABSTRACT: Localize variables to a coroutine
2 package Coro::Localize;
3 {
4 3 $Coro::Localize::VERSION = '0.1.2';
5 }
6 3 ▶use common::sense;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 3 ▶use Devel::Declare;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 3 ▶use Data::Alias ();
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 3 ▶sub import {
Subroutine import: called CC=1 SCAR=0.0
11 3 my $class = shift;
12 3 $class->import_into( scalar caller );
13 }
14  
15 3 ▶my $stub = sub {};
Subroutine __ANON__: called CC=1 SCAR=0.0
16 3 ▶sub import_into {
Subroutine import_into: called CC=1 SCAR=0.0
Pod undocumented
17 3 my $class = shift;
18 3 my( $target ) = @_;
19 3 Devel::Declare->setup_for( $target => { corolocal => {const => \&parser} } );
20 3 *{$target.'::corolocal'} = $stub;
21 }
22  
23 3 our $prefix = '';
24 78 ▶sub get {substr Devel::Declare::get_linestr, length $prefix}
Subroutine get: called CC=1 SCAR=0.0
Pod undocumented
25 48 ▶sub set { Devel::Declare::set_linestr $prefix . $_[0]}
Subroutine set: called CC=1 SCAR=0.0
Pod undocumented
26  
27 # croak that ignores Devel::Declare and our own package when determining the
28 # line to blame.
29 # Rewritten from a version in Begin::Declare
30 0 ▶sub croak {
Subroutine croak: not called CC=4 SCAR=30.0
Pod undocumented
31 0 my($msg, $src) = @_;
32  
33 0 s/\s+/ /g for $msg, $src;
34
35 0 my $yada='';
36 0 ▶ if ($src =~ s/^(.{20}).*/$1/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($src =~ s/^(.{20}).*/$1/u) 0 0 0
37 0 $yada = '...';
38 }
39  
40 0 my $thispackage = __PACKAGE__;
41  
42 0 my($ii, $package, $file, $line);
43 0 ▶ do {
Branch: 1 branch
BranchTrueFalseTotal
$package =~ /^Devel::Declare|^$thispackage/u and do { do { () = (caller ++$ii)[1, 2] } } 0 0 0
44 0 ($package,$file,$line) = (caller ++$ii)[1,2];
45 }
46 while ($package =~/^Devel::Declare|^$thispackage/);
47
48 0 die "$thispackage: $msg '$src'$yada at $file line $line.\n"
49 }
50  
51 6 ▶sub parser {
Subroutine parser: called CC=8 SCAR=20.8
Pod undocumented
52 6 local $prefix = substr get, 0, $_[1];
53 6 my $keyword = strip_keyword();
54 6 my @vars = strip_vars();
55 6 my $var_names = 'qw(' . join( ' ', @vars ). ')';
56 6 my $var_vars = '(' . join( ',', @vars) . ')';
57 6 my $assign = strip_assign();
58 6 my $code = "";
59  
60 6 $code .= q[use Data::Alias;];
61 6 $code .= q{my(%external_values,%internal_values);};
62  
63 # Initialize the thread local values to undef
64 6 $code .= q[@internal_values{qw(].join(" ",@vars).q[)} = ();];
65
66 # Do initial binding of old values to external and new to thread local
67 6 foreach (@vars) {
68 6 my $sigil = substr($_,0,1);
69 6 ▶ if ( $sigil eq '$' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($sigil eq '$') { } 2 4 6
70 2 $code .= q[alias $external_values{'].$_.q['} = ].$_.q[;];
71 }
72 else {
73 4 $code .= q[alias $external_values{'].$_.q['} = \\].$_.q[;];
74 }
75 }
76  
77 # Setup the on-enter handler
78 6 $code .= q[Coro::on_enter {];
79 6 foreach (@vars) {
80 6 my $sigil = substr($_,0,1);
81 6 ▶ if ( $sigil eq '$' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($sigil eq '$') { } 2 4 6
82 2 $code .= q[alias ].$_.q[ = ].q[$internal_values{'].$_.q['};];
83 }
84 else {
85 4 $code .= q[alias ].$_.q[ = ].$sigil.q[{ $internal_values{'].$_.q['} };];
86 }
87 }
88 6 $code .= q[};];
89  
90 # Setup the on-leave handler
91 6 $code .= q[Coro::on_leave {];
92 6 foreach (@vars) {
93 6 my $sigil = substr($_,0,1);
94 6 ▶ if ( $sigil eq '$' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($sigil eq '$') { } 2 4 6
95 2 $code .= q[alias ].$_.q[ = ].q[$external_values{'].$_.q['};];
96 }
97 else {
98 4 $code .= q[alias ].$_.q[ = ].$sigil.q[{ $external_values{'].$_.q['} };];
99 }
100 }
101 6 $code .= q[};];
102
103 6 ▶ if ( $assign ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($assign) 6 0 6
104 6 $code .= $var_vars . " $assign";
105 }
106  
107 6 set ";" . $code . get;
108 }
109  
110 30 ▶sub strip_space {
Subroutine strip_space: called CC=1 SCAR=0.0
Pod undocumented
111 30 my $skip = Devel::Declare::toke_skipspace length $prefix;
112 30 set substr get, $skip;
113 }
114  
115 6 ▶sub strip_keyword {
Subroutine strip_keyword: called CC=2 SCAR=7.0
Pod undocumented
116 6 strip_space;
117 6 ▶ get =~ /^(corolocal)(?:\b|$)/ or croak "Could not match corolocal", get;
Branch: 1 branch
BranchTrueFalseTotal
get() =~ /^(corolocal)(?:\b|$)/u or croak("Could not match corolocal", get()) 6 0 6
118 6 $prefix .= $1;
119 6 return $1;
120 }
121  
122 6 ▶sub strip_comma {
Subroutine strip_comma: called CC=2 SCAR=9.2
Pod undocumented
123 6 ▶ if ( get =~ /^,/) {
Branch: 1 branch
BranchTrueFalseTotal
if (get() =~ /^,/u) { } 0 6 6
124 0 $prefix .= ' ';
125 0 return 1;
126 }
127 else {
128 6 return 0;
129 }
130 }
131  
132 6 ▶sub strip_var {
Subroutine strip_var: called CC=3 SCAR=11.1
Pod undocumented
133 6 strip_space;
134 6 ▶ (my $line = get) =~ s/^([\$\%\@])//
Branch: 1 branch
BranchTrueFalseTotal
(my $line = get()) =~ s/^([\$\%\@])//u or croak("not a valid sigil", get() =~ /(.)/u) 6 0 6
135 or croak "not a valid sigil", get =~ /(.)/;
136 6 my $sigil = $1;
137 6 set $line;
138 6 strip_space;
139 6 ▶ ($line = get) =~ s/^(\w+|^[\S])//
Branch: 1 branch
BranchTrueFalseTotal
($line = get()) =~ s/^(\w+|^[\S])//u or croak("not a lexical variable name", $sigil . $line) 6 0 6
140 or croak "not a lexical variable name", $sigil.$line;
141 6 set $line;
142 6 $sigil . $1;
143 }
144  
145 6 ▶sub strip_open_paren {
Subroutine strip_open_paren: called CC=2 SCAR=9.2
Pod undocumented
146 6 ▶ if (get =~ /^\(/) {
Branch: 1 branch
BranchTrueFalseTotal
if (get() =~ /^\(/u) { } 0 6 6
147 0 $prefix .= ' ';
148 0 return 1;
149 }
150 else {
151 6 return 0;
152 }
153 }
154 0 ▶sub strip_close_paren {
Subroutine strip_close_paren: not called CC=2 SCAR=17.9
Pod undocumented
155 0 strip_space;
156 0 ▶ if (get =~ /^\)/) {
Branch: 1 branch
BranchTrueFalseTotal
if (get() =~ /^\)/u) { } 0 0 0
157 0 $prefix .= ' ';
158 0 return 1;
159 }
160 else {
161 0 croak "expected a close paren ')' not", get;
162 }
163 }
164  
165 6 ▶sub strip_vars {
Subroutine strip_vars: called CC=4 SCAR=17.9
Pod undocumented
166 6 strip_space;
167 6 my @vars;
168 6 ▶ if ( strip_open_paren ) {
Branch: 1 branch
BranchTrueFalseTotal
if (strip_open_paren()) { } 0 6 6
169 0 ▶ do {
Branch: 1 branch
BranchTrueFalseTotal
strip_comma() and do { do { push @vars, strip_var() } } 0 0 0
170 0 push @vars, strip_var;
171 } while (strip_comma);
172 0 strip_close_paren;
173 }
174 else {
175 6 push @vars, strip_var;
176 6 ▶ if ( strip_comma ) {
Branch: 1 branch
BranchTrueFalseTotal
if (strip_comma()) 0 6 6
177 0 croak "unexpected comma in variable declaration, maybe you meant to wrap these in parens? ", ",".get;
178 }
179 }
180 6 return @vars;
181 }
182
183
184 6 ▶sub strip_assign {
Subroutine strip_assign: called CC=3 SCAR=13.6
Pod undocumented
185 6 strip_space;
186 6 my $rest = get;
187 6 ▶ if ($rest =~ /^;/) {
Branch: 2 branches
BranchTrueFalseTotal
if ($rest =~ /^;/u) { } 0 6 6
elsif ($rest =~ /^=[^=]/u) { } 6 0 6
188 0 $prefix .= ' ';
189 0 strip_space;
190 0 return '';
191 }
192 elsif ($rest =~ /^=[^=]/) {
193 6 $prefix .= ' ' x length($rest);
194 6 return $rest;
195 }
196 else {
197 0 return '';
198 }
199 }
200  
201 3 1;
202  
203  
204 __END__
↑ Summary Syntax/Feature/Corolocal.pm »