summaryrefslogtreecommitdiffstats
path: root/lib
diff options
context:
space:
mode:
authorJesse Luehrs <doy@tozt.net>2010-10-20 15:59:09 -0500
committerJesse Luehrs <doy@tozt.net>2010-10-20 15:59:09 -0500
commit8e1b3d7b8d67f6c1bac94b4f59cfca2d097c3240 (patch)
tree46a43387b36004c151947d65642088e2950fe925 /lib
parent3eb05ecbc5a5af77aa3397992bfbbf22c7a5a86e (diff)
downloadeval-closure-8e1b3d7b8d67f6c1bac94b4f59cfca2d097c3240.tar.gz
eval-closure-8e1b3d7b8d67f6c1bac94b4f59cfca2d097c3240.zip
move the validation of environment to the beginning
we can't reliably throw errors from inside _clean_eval_closure, since we localize $@
Diffstat (limited to 'lib')
-rw-r--r--lib/Eval/Closure.pm23
1 files changed, 19 insertions, 4 deletions
diff --git a/lib/Eval/Closure.pm b/lib/Eval/Closure.pm
index f0457fd..fbd3e2c 100644
--- a/lib/Eval/Closure.pm
+++ b/lib/Eval/Closure.pm
@@ -11,7 +11,9 @@ use Try::Tiny;
sub eval_closure {
my (%args) = @_;
+
$args{source} = _canonicalize_source($args{source});
+ _validate_env($args{environment} ||= {});
my ($code, $e) = _clean_eval_closure(@args{qw(source environment name)});
@@ -47,6 +49,20 @@ sub _canonicalize_source {
}
}
+sub _validate_env {
+ my ($env) = @_;
+
+ croak("The 'environment' parameter must be a hashref")
+ unless reftype($env) eq 'HASH';
+
+ for my $var (keys %$env) {
+ croak("Environment key '$_' should start with \@, \%, or \$")
+ unless $var =~ /^([\@\%\$])/;
+ croak("Environment values must be references, not $env->{$var}")
+ unless ref($env->{$var});
+ }
+}
+
sub _clean_eval_closure {
# my ($source, $__captures, $name) = @_
my $__captures = $_[1];
@@ -74,10 +90,9 @@ sub _make_source {
my ($source, $__captures) = @_;
return join "\n", (
(map {
- die "Capture key should start with \@, \% or \$: $_"
- unless /^([\@\%\$])/;
- 'my ' . $_ . ' = ' . $1 . '{$__captures->{\'' . $_ . '\'}};';
- } keys %$__captures),
+ 'my ' . $_ . ' = '
+ . substr($_, 0, 1) . '{$__captures->{\'' . $_ . '\'}};'
+ } keys %$__captures),
$source,
);
}