4 use Sub::Exporter -setup => {
5 exports => [qw(eval_closure)],
6 groups => { default => [qw(eval_closure)] },
8 # ABSTRACT: safely and cleanly create closures via string eval
12 use Scalar::Util qw(reftype);
19 my $code = eval_closure(
20 source => 'sub { $foo++ }',
29 my $code2 = eval_closure(
30 source => 'sub { $code->() }',
31 ); # dies, $code isn't in scope
35 String eval is often used for dynamic code generation. For instance, C<Moose>
36 uses it heavily, to generate inlined versions of accessors and constructors,
37 which speeds code up at runtime by a significant amount. String eval is not
38 without its issues however - it's difficult to control the scope it's used in
39 (which determines which variables are in scope inside the eval).
41 This module attempts to solve this problem. It provides an C<eval_closure>
42 function, which evals a string in a clean environment, other than a fixed list
43 of specified variables.
47 =func eval_closure(%args)
49 This function provides the main functionality of this module. It is exported by
50 default. It takes a hash of parameters, with these keys being valid:
56 The string to be evaled. It should end by returning a code reference. It can
57 access any variable declared in the C<environment> parameter (and only those
58 variables). It can be either a string, or an arrayref of lines (which will be
59 joined with newlines to produce the string).
63 The environment to provide to the eval. This should be a hashref, mapping
64 variable names (including sigils) to references of the appropriate type. For
65 instance, a valid value for environment would be C<< { '@foo' => [] } >> (which
66 would allow the generated function to use an array named C<@foo>). Generally,
67 this is used to allow the generated function to access externally defined
68 variables (so you would pass in a reference to a variable that already exists).
72 This lets you provide a bit more information in backtraces. Normally, when a
73 function that was generated through string eval is called, that stack frame
74 will show up as "(eval n)", where 'n' is a sequential identifier for every
75 string eval that has happened so far in the program. Passing a C<description>
76 parameter lets you override that to something more useful (for instance,
77 L<Moose> overrides the description for accessors to something like "accessor
78 foo at MyClass.pm, line 123").
82 This lets you override the particular line number that appears in backtraces,
83 much like the C<description> option. The default is 1.
87 Normally, this function appends the source code that failed to compile, and
88 prepends some explanatory text. Setting this option to true suppresses that
89 behavior so you get only the compilation error that Perl actually reported.
98 $args{source} = _canonicalize_source($args{source});
99 _validate_env($args{environment} ||= {});
101 $args{source} = _line_directive(@args{qw(line description)})
103 if defined $args{description} && !($^P & 0x10);
105 my ($code, $e) = _clean_eval_closure(@args{qw(source environment)});
108 if ($args{terse_error}) {
112 croak("Failed to compile source: $e\n\nsource:\n$args{source}")
119 sub _canonicalize_source {
122 if (defined($source)) {
124 if (reftype($source) eq 'ARRAY'
125 || overload::Method($source, '@{}')) {
126 return join "\n", @$source;
128 elsif (overload::Method($source, '""')) {
132 croak("The 'source' parameter to eval_closure must be a "
133 . "string or array reference");
141 croak("The 'source' parameter to eval_closure is required");
148 croak("The 'environment' parameter must be a hashref")
149 unless reftype($env) eq 'HASH';
151 for my $var (keys %$env) {
152 croak("Environment key '$var' should start with \@, \%, or \$")
153 unless $var =~ /^([\@\%\$])/;
154 croak("Environment values must be references, not $env->{$var}")
155 unless ref($env->{$var});
159 sub _line_directive {
160 my ($line, $description) = @_;
162 $line = 1 unless defined($line);
164 return qq{#line $line "$description"\n};
167 sub _clean_eval_closure {
168 my ($source, $captures) = @_;
170 my @capture_keys = sort keys %$captures;
172 if ($ENV{EVAL_CLOSURE_PRINT_SOURCE}) {
173 _dump_source(_make_compiler_source($source, @capture_keys));
176 my ($compiler, $e) = _make_compiler($source, @capture_keys);
178 if (defined $compiler) {
179 $code = $compiler->(@$captures{@capture_keys});
182 if (defined($code) && (!ref($code) || ref($code) ne 'CODE')) {
183 $e = "The 'source' parameter must return a subroutine reference, "
192 my $source = _make_compiler_source(@_);
194 return @{ _clean_eval($source) };
197 $Eval::Closure::SANDBOX_ID = 0;
200 $Eval::Closure::SANDBOX_ID++;
202 package Eval::Closure::Sandbox_$Eval::Closure::SANDBOX_ID;
204 local \$SIG{__DIE__};
205 my \$compiler = eval \$_[0];
211 sub _make_compiler_source {
212 my ($source, @capture_keys) = @_;
217 'my ' . $_ . ' = ' . substr($_, 0, 1) . '{$_[' . $i++ . ']};'
228 if (try { require Perl::Tidy }) {
229 Perl::Tidy::perltidy(
231 destination => \$output,
246 Please report any bugs through RT: email
247 C<bug-eval-closure at rt.cpan.org>, or browse to
248 L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Eval-Closure>.
254 =item * L<Class::MOP::Method::Accessor>
256 This module is a factoring out of code that used to live here
262 You can find this documentation for this module with the perldoc command.
264 perldoc Eval::Closure
266 You can also look for information at:
270 =item * AnnoCPAN: Annotated CPAN documentation
272 L<http://annocpan.org/dist/Eval-Closure>
276 L<http://cpanratings.perl.org/d/Eval-Closure>
278 =item * RT: CPAN's request tracker
280 L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=Eval-Closure>
284 L<http://search.cpan.org/dist/Eval-Closure>
290 Jesse Luehrs <doy at tozt dot net>
292 Based on code from L<Class::MOP::Method::Accessor>, by Stevan Little and the