summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorShawn M Moore <sartak@bestpractical.com>2011-01-26 22:19:50 -0500
committerShawn M Moore <sartak@bestpractical.com>2011-01-26 22:21:05 -0500
commit75e6988b5ef9fd1dd384d0bf3a74e50178683d17 (patch)
tree2e51c0bcd943c2e0213712653551f3df917722b0
parent5617e9667fce453ffa66029ebc10c972a3066725 (diff)
downloadeval-closure-75e6988b5ef9fd1dd384d0bf3a74e50178683d17.tar.gz
eval-closure-75e6988b5ef9fd1dd384d0bf3a74e50178683d17.zip
Add a line option for controlling that bit as well
-rw-r--r--lib/Eval/Closure.pm13
-rw-r--r--t/03-description.t13
2 files changed, 23 insertions, 3 deletions
diff --git a/lib/Eval/Closure.pm b/lib/Eval/Closure.pm
index 6a1f41e..0bb553e 100644
--- a/lib/Eval/Closure.pm
+++ b/lib/Eval/Closure.pm
@@ -83,6 +83,11 @@ 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, 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
@@ -99,7 +104,7 @@ 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)});
@@ -157,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 {
diff --git a/t/03-description.t b/t/03-description.t
index 97f8372..15a2ce1 100644
--- a/t/03-description.t
+++ b/t/03-description.t
@@ -37,4 +37,17 @@ SOURCE
);
}
+{
+ my $code = eval_closure(
+ source => $source,
+ line => 100,
+ description => 'accessor foo (defined at Class.pm line 282)',
+ );
+
+ like(
+ exception { $code->() },
+ qr/^foo at accessor foo \(defined at Class\.pm line 282\) line 101\n/,
+ "description is set"
+ );
+}
done_testing;