blib/lib/Eval/Closure.pm

filter: Statementstmt 91.4% 96 / 105 Branchbran 86.8% 33 / 38 Conditioncond 78.5% 11 / 14 MC/DCmcdc 50.0% 4 / 8 Subroutinesub 90.4% 19 / 21 Podpod 100% 1 / 1 totaltotal 87.7% 164 / 187 CC 27 SCAR 14.6
CC
27
Cov
89%
_clean_eval_closure
8.2
eval_closure
7.0
_canonicalize_source
6.1
CRAP 4.3
↑ Summary
1 package Eval::Closure;
2 BEGIN {
3 13 ▶ $Eval::Closure::AUTHORITY = 'cpan:DOY';
Subroutine BEGIN: called CC=1 SCAR=0.0
4 }
5 11 $Eval::Closure::VERSION = '0.13';
6 13 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 12 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 # ABSTRACT: safely and cleanly create closures via string eval
9  
10 12 ▶use Exporter 'import';
Subroutine BEGIN: called CC=1 SCAR=0.0
11 11 @Eval::Closure::EXPORT = @Eval::Closure::EXPORT_OK = 'eval_closure';
12  
13 12 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 11 ▶use overload ();
Subroutine BEGIN: called CC=1 SCAR=0.0
15 11 ▶use Scalar::Util qw(reftype);
Subroutine BEGIN: called CC=1 SCAR=0.0
16 11 ▶use Try::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
17  
18 11 ▶use constant HAS_LEXICAL_SUBS => $] >= 5.018;
Subroutine BEGIN: called CC=1 SCAR=0.0
19  
20  
21  
22 sub eval_closure {
23 29 ▶ my (%args) = @_;
Subroutine eval_closure: called CC=7 SCAR=19.5
Pod documented
24  
25 # default to copying environment
26 29 ▶ $args{alias} = 0 if !exists $args{alias};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $args{"alias"} 28 1 29
27  
28 29 $args{source} = _canonicalize_source($args{source});
29 27 ▶ _validate_env($args{environment} ||= {});
Condition100%2/2
$args{"environment"} ||= {}
l!l
720
MC/DC100%1/1
$args{"environment"} ||= {}
$args{"environment"}
Truth table100%2/2
A
A= $args{"environment"}
Aresult
11
00
30  
31 $args{source} = _line_directive(@args{qw(line description)})
32 . $args{source}
33 25 ▶ if defined $args{description} && !($^P & 0x10);
Branch: 1 branch
BranchTrueFalseTotal
if defined $args{"description"} and not $^P & 16 2 23 25
Condition100%3/3
defined $args{"description"} and not $^P & 16
!ll&&!rl&&r
2212
MC/DC100%2/2
defined $args{"description"} and not $^P & 16
defined $args{"description"} not $^P & 16
Truth table100%3/3
A and not(B)
A= defined $args{"description"}B= not $^P & 16
ABresult
0X0
100
111
34  
35 25 my ($code, $e) = _clean_eval_closure(@args{qw(source environment alias)});
36  
37 24 ▶ if (!$code) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($code) 6 18 24
38 6 ▶ if ($args{terse_error}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($args{'terse_error'}) { } 1 5 6
39 1 die "$e\n";
40 }
41 else {
42 5 croak("Failed to compile source: $e\n\nsource:\n$args{source}")
43 }
44 }
45  
46 18 return $code;
47 }
48  
49 sub _canonicalize_source {
50 29 ▶ my ($source) = @_;
Subroutine _canonicalize_source: called CC=6 SCAR=18.1
51  
52 29 ▶ if (defined($source)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $source) { } 28 1 29
53 28 ▶ if (ref($source)) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $source) { } 2 26 28
54 2 ▶ if (reftype($source) eq 'ARRAY'
Branch: 2 branches
BranchTrueFalseTotal
if (builtin::reftype($source) eq 'ARRAY' or overload::Method($source, '@{}')) { } 1 1 2
elsif (overload::Method($source, '""')) { } 0 1 1
Condition66%2/3
builtin::reftype($source) eq "ARRAY" or overload::Method($source, "\@{}")
l!l&&r!l&&!r
101
MC/DC50%1/2
builtin::reftype($source) eq "ARRAY" or overload::Method($source, "\@{}")
builtin::reftype($source) eq "ARRAY" overload::Method($source, "\@{}")
Truth table66%2/3
A or B
A= builtin::reftype($source) eq "ARRAY"B= overload::Method($source, "\@{}")
ABresult
1X1
011
000
55 || overload::Method($source, '@{}')) {
56 1 return join "\n", @$source;
57 }
58 elsif (overload::Method($source, '""')) {
59 0 return "$source";
60 }
61 else {
62 1 croak("The 'source' parameter to eval_closure must be a "
63 . "string or array reference");
64 }
65 }
66 else {
67 26 return $source;
68 }
69 }
70 else {
71 1 croak("The 'source' parameter to eval_closure is required");
72 }
73 }
74  
75 sub _validate_env {
76 27 ▶ my ($env) = @_;
Subroutine _validate_env: called CC=5 SCAR=16.1
77  
78 27 ▶ croak("The 'environment' parameter must be a hashref")
Branch: 1 branch
BranchTrueFalseTotal
unless builtin::reftype($env) eq "HASH" 0 27 27
79 unless reftype($env) eq 'HASH';
80  
81 27 for my $var (keys %$env) {
82 8 if (HAS_LEXICAL_SUBS) {
83 8 ▶ croak("Environment key '$var' should start with \@, \%, \$, or \&")
Branch: 1 branch
BranchTrueFalseTotal
unless $var =~ /^([\@\%\$\&])/ 1 7 8
84 unless $var =~ /^([\@\%\$\&])/;
85 }
86 else {
87 croak("Environment key '$var' should start with \@, \%, or \$")
88 unless $var =~ /^([\@\%\$])/;
89 }
90 croak("Environment values must be references, not $env->{$var}")
91 7 ▶ unless ref($env->{$var});
Branch: 1 branch
BranchTrueFalseTotal
unless ref $env->{$var} 1 6 7
92 }
93 }
94  
95 sub _line_directive {
96 2 ▶ my ($line, $description) = @_;
Subroutine _line_directive: called CC=2 SCAR=6.9
97  
98 2 ▶ $line = 1 unless defined($line);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $line 1 1 2
99  
100 2 return qq{#line $line "$description"\n};
101 }
102  
103 sub _clean_eval_closure {
104 25 ▶ my ($source, $captures, $alias) = @_;
Subroutine _clean_eval_closure: called CC=8 SCAR=21.0
105  
106 25 my @capture_keys = sort keys %$captures;
107  
108 25 ▶ if ($ENV{EVAL_CLOSURE_PRINT_SOURCE}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ENV{"EVAL_CLOSURE_PRINT_SOURCE"}) 0 25 25
109 0 _dump_source(_make_compiler_source($source, $alias, @capture_keys));
110 }
111  
112 25 my ($compiler, $e) = _make_compiler($source, $alias, @capture_keys);
113 25 my $code;
114 25 ▶ if (defined $compiler) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $compiler) 20 5 25
115 20 $code = $compiler->(@$captures{@capture_keys});
116 }
117  
118 24 ▶ if (defined($code) && (!ref($code) || ref($code) ne 'CODE')) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $code and !ref($code) || ref $code ne "CODE") 1 23 24
Condition66%4/6
defined $code and !ref($code) || ref $code ne "CODE"
!ll&&!rl&&r
5190
not ref $code or ref $code ne "CODE"
l!l&&r!l&&!r
1018
MC/DC0%0/3
defined $code and !ref($code) || ref $code ne "CODE"
defined $code not ref $code ref $code ne "CODE"
Truth table50%2/4
A and not(B) or C
A= defined $codeB= not ref $codeC= ref $code ne "CODE"
ABCresult
0XX0
1000
11X1
1011
119 1 $e = "The 'source' parameter must return a subroutine reference, "
120 . "not $code";
121 1 undef $code;
122 }
123  
124 24 ▶ if ($alias) {
Branch: 1 branch
BranchTrueFalseTotal
if ($alias) 1 23 24
125 1 require Devel::LexAlias;
126 Devel::LexAlias::lexalias($code, $_, $captures->{$_})
127 1 for grep !/^\&/, keys %$captures;
128 }
129  
130 24 return ($code, $e);
131 }
132  
133 sub _make_compiler {
134 25 ▶ my $source = _make_compiler_source(@_);
Subroutine _make_compiler: called CC=1 SCAR=0.0
135  
136 25 return @{ _clean_eval($source) };
137 }
138  
139 sub _clean_eval {
140 25 ▶ local $@;
Subroutine _clean_eval: called CC=1 SCAR=0.0
141 25 local $SIG{__DIE__};
142 25 ▶ my $compiler = eval $_[0];
Subroutine BEGIN: called CC=1 SCAR=0.0
143 25 my $e = $@;
144 25 [ $compiler, $e ];
145 }
146  
147 11 $Eval::Closure::SANDBOX_ID = 0;
148  
149 sub _make_compiler_source {
150 25 ▶ my ($source, $alias, @capture_keys) = @_;
Subroutine _make_compiler_source: called CC=1 SCAR=0.0
151 25 $Eval::Closure::SANDBOX_ID++;
152 25 my $i = 0;
153 return join "\n", (
154 "package Eval::Closure::Sandbox_$Eval::Closure::SANDBOX_ID;",
155 'sub {',
156 25 (map { _make_lexical_assignment($_, $i++, $alias) } @capture_keys),
157 $source,
158 '}',
159 );
160 }
161  
162 sub _make_lexical_assignment {
163 6 ▶ my ($key, $index, $alias) = @_;
Subroutine _make_lexical_assignment: called CC=3 SCAR=11.0
164 6 my $sigil = substr($key, 0, 1);
165 6 my $name = substr($key, 1);
166 6 ▶ if (HAS_LEXICAL_SUBS && $sigil eq '&') {
Branch: 1 branch
BranchTrueFalseTotal
if ($sigil eq "&") 1 5 6
167 1 my $tmpname = '$__' . $name . '__' . $index;
168 1 return 'use feature "lexical_subs"; '
169 . 'no warnings "experimental::lexical_subs"; '
170 . 'my ' . $tmpname . ' = $_[' . $index . ']; '
171 . 'my sub ' . $name . ' { goto ' . $tmpname . ' }';
172 }
173 5 ▶ if ($alias) {
Branch: 1 branch
BranchTrueFalseTotal
if ($alias) { } 1 4 5
174 1 return 'my ' . $key . ';';
175 }
176 else {
177 4 return 'my ' . $key . ' = ' . $sigil . '{$_[' . $index . ']};';
178 }
179 }
180  
181 sub _dump_source {
182 0 ▶ my ($source) = @_;
Subroutine _dump_source: not called CC=2 SCAR=17.9
183  
184 0 my $output;
185 0 ▶ if (try { require Perl::Tidy }) {
Subroutine __ANON__: not called CC=1 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
if (&try(sub { require Perl::Tidy; } )) { } 0 0 0
186 0 Perl::Tidy::perltidy(
187 source => \$source,
188 destination => \$output,
189 argv => [],
190 );
191 }
192 else {
193 0 $output = $source;
194 }
195  
196 0 warn "$output\n";
197 }
198  
199  
200 11 1;
201  
202 __END__
203  
204 =pod
205  
206 =encoding UTF-8
207  
208 =head1 NAME
209  
210 Eval::Closure - safely and cleanly create closures via string eval
211  
212 =head1 VERSION
213  
214 version 0.13
215  
216 =head1 SYNOPSIS
217  
218 use Eval::Closure;
219  
220 my $code = eval_closure(
221 source => 'sub { $foo++ }',
222 environment => {
223 '$foo' => \1,
224 },
225 );
226  
227 warn $code->(); # 1
228 warn $code->(); # 2
229  
230 my $code2 = eval_closure(
231 source => 'sub { $code->() }',
232 ); # dies, $code isn't in scope
233  
234 =head1 DESCRIPTION
235  
236 String eval is often used for dynamic code generation. For instance, C<Moose>
237 uses it heavily, to generate inlined versions of accessors and constructors,
238 which speeds code up at runtime by a significant amount. String eval is not
239 without its issues however - it's difficult to control the scope it's used in
240 (which determines which variables are in scope inside the eval), and it's easy
241 to miss compilation errors, since eval catches them and sticks them in $@
242 instead.
243  
244 This module attempts to solve these problems. It provides an C<eval_closure>
245 function, which evals a string in a clean environment, other than a fixed list
246 of specified variables. Compilation errors are rethrown automatically.
247  
248 =head1 FUNCTIONS
249  
250 =head2 eval_closure(%args)
251  
252 This function provides the main functionality of this module. It is exported by
253 default. It takes a hash of parameters, with these keys being valid:
254  
255 =over 4
256  
257 =item source
258  
259 The string to be evaled. It should end by returning a code reference. It can
260 access any variable declared in the C<environment> parameter (and only those
261 variables). It can be either a string, or an arrayref of lines (which will be
262 joined with newlines to produce the string).
263  
264 =item environment
265  
266 The environment to provide to the eval. This should be a hashref, mapping
267 variable names (including sigils) to references of the appropriate type. For
268 instance, a valid value for environment would be C<< { '@foo' => [] } >> (which
269 would allow the generated function to use an array named C<@foo>). Generally,
270 this is used to allow the generated function to access externally defined
271 variables (so you would pass in a reference to a variable that already exists).
272  
273 In perl 5.18 and greater, the environment hash can contain variables with a
274 sigil of C<&>. This will create a lexical sub in the evaluated code (see
275 L<feature/The 'lexical_subs' feature>). Using a C<&> sigil on perl versions
276 before lexical subs were available will throw an error.
277  
278 =item alias
279  
280 If set to true, the coderef returned closes over the variables referenced in
281 the environment hashref. (This feature requires L<Devel::LexAlias>.) If set to
282 false, the coderef closes over a I<< shallow copy >> of the variables.
283  
284 If this argument is omitted, Eval::Closure will currently assume false, but
285 this assumption may change in a future version.
286  
287 =item description
288  
289 This lets you provide a bit more information in backtraces. Normally, when a
290 function that was generated through string eval is called, that stack frame
291 will show up as "(eval n)", where 'n' is a sequential identifier for every
292 string eval that has happened so far in the program. Passing a C<description>
293 parameter lets you override that to something more useful (for instance,
294 L<Moose> overrides the description for accessors to something like "accessor
295 foo at MyClass.pm, line 123").
296  
297 =item line
298  
299 This lets you override the particular line number that appears in backtraces,
300 much like the C<description> option. The default is 1.
301  
302 =item terse_error
303  
304 Normally, this function appends the source code that failed to compile, and
305 prepends some explanatory text. Setting this option to true suppresses that
306 behavior so you get only the compilation error that Perl actually reported.
307  
308 =back
309  
310 =head1 BUGS
311  
312 No known bugs.
313  
314 Please report any bugs to GitHub Issues at
315 L<https://github.com/doy/eval-closure/issues>.
316  
317 =head1 SEE ALSO
318  
319 =over 4
320  
321 =item * L<Class::MOP::Method::Accessor>
322  
323 This module is a factoring out of code that used to live here
324  
325 =back
326  
327 =head1 SUPPORT
328  
329 You can find this documentation for this module with the perldoc command.
330  
331 perldoc Eval::Closure
332  
333 You can also look for information at:
334  
335 =over 4
336  
337 =item * MetaCPAN
338  
339 L<https://metacpan.org/release/Eval-Closure>
340  
341 =item * Github
342  
343 L<https://github.com/doy/eval-closure>
344  
345 =item * RT: CPAN's request tracker
346  
347 L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=Eval-Closure>
348  
349 =item * CPAN Ratings
350  
351 L<http://cpanratings.perl.org/d/Eval-Closure>
352  
353 =back
354  
355 =head1 NOTES
356  
357 Based on code from L<Class::MOP::Method::Accessor>, by Stevan Little and the
358 Moose Cabal.
359  
360 =head1 AUTHOR
361  
362 Jesse Luehrs <doy@tozt.net>
363  
364 =head1 COPYRIGHT AND LICENSE
365  
366 This software is copyright (c) 2015 by Jesse Luehrs.
367  
368 This is free software; you can redistribute it and/or modify it under
369 the same terms as the Perl 5 programming language system itself.
370  
371 =cut
↑ Summary