docs
[gitmo/Eval-Closure.git] / lib / Eval / Closure.pm
index 01e1411..ccd2bbc 100644 (file)
@@ -9,7 +9,6 @@ use Sub::Exporter -setup => {
 
 use Carp;
 use overload ();
-use Memoize;
 use Scalar::Util qw(reftype);
 use Try::Tiny;
 
@@ -37,16 +36,13 @@ String eval is often used for dynamic code generation. For instance, C<Moose>
 uses it heavily, to generate inlined versions of accessors and constructors,
 which speeds code up at runtime by a significant amount. String eval is not
 without its issues however - it's difficult to control the scope it's used in
-(which determines which variables are in scope inside the eval), and it can be
-quite slow, especially if doing a large number of evals.
+(which determines which variables are in scope inside the eval), and it's easy
+to miss compilation errors, since eval catches them and sticks them in $@
+instead.
 
-This module attempts to solve both of those problems. It provides an
-C<eval_closure> function, which evals a string in a clean environment, other
-than a fixed list of specified variables. It also caches the result of the
-eval, so that doing repeated evals of the same source, even with a different
-environment, will be much faster (but note that the description is part of the
-string to be evaled, so it must also be the same (or non-existent) if caching
-is to work properly).
+This module attempts to solve these problems. It provides an C<eval_closure>
+function, which evals a string in a clean environment, other than a fixed list
+of specified variables. Compilation errors are rethrown automatically.
 
 =cut
 
@@ -106,7 +102,7 @@ sub eval_closure {
 
     $args{source} = _line_directive(@args{qw(line description)})
                   . $args{source}
-        if defined $args{description};
+        if defined $args{description} && !($^P & 0x10);
 
     my ($code, $e) = _clean_eval_closure(@args{qw(source environment)});
 
@@ -171,13 +167,14 @@ sub _line_directive {
 }
 
 sub _clean_eval_closure {
-     my ($source, $captures) = @_;
+    my ($source, $captures) = @_;
+
+    my @capture_keys = sort keys %$captures;
 
     if ($ENV{EVAL_CLOSURE_PRINT_SOURCE}) {
-        _dump_source(_make_compiler_source(@_));
+        _dump_source(_make_compiler_source($source, @capture_keys));
     }
 
-    my @capture_keys = sort keys %$captures;
     my ($compiler, $e) = _make_compiler($source, @capture_keys);
     my $code;
     if (defined $compiler) {
@@ -194,13 +191,24 @@ sub _clean_eval_closure {
 }
 
 sub _make_compiler {
-    local $@;
-    local $SIG{__DIE__};
-    my $compiler = eval _make_compiler_source(@_);
-    my $e = $@;
-    return ($compiler, $e);
+    my $source = _make_compiler_source(@_);
+
+    return @{ _clean_eval($source) };
+}
+
+$Eval::Closure::SANDBOX_ID = 0;
+
+sub _clean_eval {
+    $Eval::Closure::SANDBOX_ID++;
+    return eval <<EVAL;
+package Eval::Closure::Sandbox_$Eval::Closure::SANDBOX_ID;
+local \$@;
+local \$SIG{__DIE__};
+my \$compiler = eval \$_[0];
+my \$e = \$@;
+[ \$compiler, \$e ];
+EVAL
 }
-memoize('_make_compiler');
 
 sub _make_compiler_source {
     my ($source, @capture_keys) = @_;
@@ -223,6 +231,7 @@ sub _dump_source {
         Perl::Tidy::perltidy(
             source      => \$source,
             destination => \$output,
+            argv        => [],
         );
     }
     else {