summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJesse Luehrs <doy@tozt.net>2010-10-20 16:59:21 -0500
committerJesse Luehrs <doy@tozt.net>2010-10-20 16:59:21 -0500
commit3efcc0874f36b6ce7acf839f017fc789abc40a8c (patch)
tree4f4cc83a371e9f8a0d0f24ba5ce4081d3ee06504
parent9736bf12e7521f7d8a4587c6c4be9c47da39c7cf (diff)
downloadeval-closure-3efcc0874f36b6ce7acf839f017fc789abc40a8c.tar.gz
eval-closure-3efcc0874f36b6ce7acf839f017fc789abc40a8c.zip
allow adding #line directives
-rw-r--r--lib/Eval/Closure.pm9
-rw-r--r--t/03-description.t38
2 files changed, 47 insertions, 0 deletions
diff --git a/lib/Eval/Closure.pm b/lib/Eval/Closure.pm
index c2a4387..5094d0a 100644
--- a/lib/Eval/Closure.pm
+++ b/lib/Eval/Closure.pm
@@ -17,6 +17,9 @@ sub eval_closure {
$args{source} = _canonicalize_source($args{source});
_validate_env($args{environment} ||= {});
+ $args{source} = _line_directive($args{description}) . $args{source}
+ if defined $args{description};
+
my ($code, $e) = _clean_eval_closure(@args{qw(source environment name)});
croak("Failed to compile source: $e\n\nsource:\n$args{source}")
@@ -65,6 +68,12 @@ sub _validate_env {
}
}
+sub _line_directive {
+ my ($description) = @_;
+
+ return qq{#line 1 "$description"\n};
+}
+
sub _clean_eval_closure {
# my ($source, $__captures, $name) = @_
my $__captures = $_[1];
diff --git a/t/03-description.t b/t/03-description.t
new file mode 100644
index 0000000..781ec72
--- /dev/null
+++ b/t/03-description.t
@@ -0,0 +1,38 @@
+#!/usr/bin/env perl
+use strict;
+use warnings;
+use Test::More;
+use Test::Exception;
+
+use Eval::Closure;
+
+my $source = <<'SOURCE';
+sub {
+ Carp::confess("foo")
+}
+SOURCE
+
+{
+ my $code = eval_closure(
+ source => $source,
+ );
+
+ throws_ok {
+ $code->();
+ } qr/^foo at \(eval \d+\) line 2\n/,
+ "no location info if context isn't passed";
+}
+
+{
+ my $code = eval_closure(
+ source => $source,
+ description => 'accessor foo (defined at Class.pm line 282)',
+ );
+
+ throws_ok {
+ $code->();
+ } qr/^foo at accessor foo \(defined at Class\.pm line 282\) line 2\n/,
+ "description is set";
+}
+
+done_testing;