From: Shawn M Moore Date: Thu, 27 Jan 2011 02:31:55 +0000 (-0500) Subject: Add a terse_error parameter to eval_closure X-Git-Tag: 0.02~3 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=5617e9667fce453ffa66029ebc10c972a3066725;p=gitmo%2FEval-Closure.git Add a terse_error parameter to eval_closure --- diff --git a/lib/Eval/Closure.pm b/lib/Eval/Closure.pm index 48f9258..6a1f41e 100644 --- a/lib/Eval/Closure.pm +++ b/lib/Eval/Closure.pm @@ -83,6 +83,12 @@ parameter lets you override that to something more useful (for instance, L overrides the description for accessors to something like "accessor foo at MyClass.pm, line 123"). +=item terse_error + +Normally, this function appends the source code that failed to compile, and +prepends some explanatory text. Setting this option to true suppresses that +behavior so you get only the compilation error that Perl actually reported. + =back =cut @@ -98,8 +104,14 @@ sub eval_closure { my ($code, $e) = _clean_eval_closure(@args{qw(source environment)}); - croak("Failed to compile source: $e\n\nsource:\n$args{source}") - unless $code; + if (!$code) { + if ($args{terse_error}) { + die "$e\n"; + } + else { + croak("Failed to compile source: $e\n\nsource:\n$args{source}") + } + } return $code; } diff --git a/t/10-errors.t b/t/10-errors.t index e724e78..905d6c8 100644 --- a/t/10-errors.t +++ b/t/10-errors.t @@ -52,4 +52,16 @@ like( "gives us compile errors properly" ); +like( + exception { eval_closure(source => 'sub { $x }') }, + qr/sub \s* { \s* \$x \s* }/x, + "without terse_error, includes the source code" +); + +unlike( + exception { eval_closure(source => 'sub { $x }', terse_error => 1) }, + qr/sub \s* { \s* \$x \s* }/x, + "with terse_error, does not include the source code" +); + done_testing;