Add a line option for controlling that bit as well
[gitmo/Eval-Closure.git] / lib / Eval / Closure.pm
index b03df5b..0bb553e 100644 (file)
@@ -81,7 +81,18 @@ will show up as "(eval n)", where 'n' is a sequential identifier for every
 string eval that has happened so far in the program. Passing a C<description>
 parameter lets you override that to something more useful (for instance,
 L<Moose> overrides the description for accessors to something like "accessor
-foo at MyClass.pm, like 123").
+foo at MyClass.pm, line 123").
+
+=item line
+
+This lets you override the particular line number that appears in backtraces,
+much like the description option. The default is "1".
+
+=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
 
@@ -93,13 +104,19 @@ sub eval_closure {
     $args{source} = _canonicalize_source($args{source});
     _validate_env($args{environment} ||= {});
 
-    $args{source} = _line_directive($args{description}) . $args{source}
+    $args{source} = _line_directive(@args{qw(line description)}) . $args{source}
         if defined $args{description};
 
     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;
 }
@@ -145,9 +162,11 @@ sub _validate_env {
 }
 
 sub _line_directive {
-    my ($description) = @_;
+    my ($line, $description) = @_;
+
+    $line = 1 if !defined($line);
 
-    return qq{#line 1 "$description"\n};
+    return qq{#line $line "$description"\n};
 }
 
 sub _clean_eval_closure {