From 9e4b587dca809519106f004df09f5aa01b721e9a Mon Sep 17 00:00:00 2001 From: Jesse Luehrs Date: Tue, 23 Jul 2013 03:04:36 -0400 Subject: don't return a coderef if there was a parse error it won't be a valid coderef, you'll just get an error if you try to call it --- Keyword.xs | 7 ++++++- t/error.t | 33 +++++++++++++++++++++++++++++++++ 2 files changed, 39 insertions(+), 1 deletion(-) create mode 100644 t/error.t diff --git a/Keyword.xs b/Keyword.xs index 7f70222..b52f98c 100644 --- a/Keyword.xs +++ b/Keyword.xs @@ -22,12 +22,17 @@ static SV *parser_fn(OP *(fn)(pTHX_ U32), bool named) { I32 floor; + OP *parsed; CV *code; REENTER_PARSER; floor = start_subparse(0, named ? 0 : CVf_ANON); - code = newATTRSUB(floor, NULL, NULL, NULL, fn(aTHX_ 0)); + parsed = fn(aTHX_ 0); + if (PL_parser->error_count) { + return newSV(0); + } + code = newATTRSUB(floor, NULL, NULL, NULL, parsed); LEAVE_PARSER; diff --git a/t/error.t b/t/error.t new file mode 100644 index 0000000..08a478a --- /dev/null +++ b/t/error.t @@ -0,0 +1,33 @@ +#!/usr/bin/env perl +use strict; +use warnings; +use Test::More; + +my $got_code; +BEGIN { + package My::Parser; + use Exporter 'import'; + our @EXPORT = 'foo'; + use Parse::Keyword { foo => \&parse_foo }; + + sub foo {} + sub parse_foo { + lex_read_space; + my $code = parse_block; + $got_code = $code ? 1 : 0; + return sub {}; + } + + $INC{'My/Parser.pm'} = __FILE__; +} + +use My::Parser; + +eval "foo"; +ok($@); +ok(!$got_code); +eval "foo { }"; +ok(!$@); +ok($got_code); + +done_testing; -- cgit v1.2.3-54-g00ecf