I'm working on a grammer that I would like to do some tests that I can't quite figure out how to do with the normal P::RD grammer tests.

Yes, I know I should figure out how to implement my tests with a more detailed grammer... until I get around to that, I'm hoping I can figure out how to modify the error message from the action block.

I know how to fail parsing, just set $return to undef. However, I'm having a difficult time modifying the error message produced by P::RD.

Getting to the error production from inside the action isn't difficult since the error production is the last production in the rule:

$thisrule->{'prods'}[ $#{ $thisrule->{'prods'} } ]
Which, in my example is:
$VAR1 = bless( { 'number' => '1', 'strcount' => 0, 'dircount' => 1, 'uncommit' => 0, 'error' => 1, 'patcount' => 0, 'actcount' => 0, 'items' => [ bless( { 'msg' => ' my message here', 'hashname' => '__DIRECTIVE1__', 'commitonly' => '', 'lookahead' => 0, 'line' => 82 }, 'Parse::RecDescent::Error' ) ], 'line' => 82 }, 'Parse::RecDescent::Production' );
I should be able to (though not very nicely) get to msg via this:
$thisrule->{'prods'}[ $#{ $thisrule->{'prods'} } ]->{'items'}- +>[0]->{'msg'}
Setting that to be a different value, then triggering the specific error I'm looking for causes the error message to be changed, in the data structure (so says DD) however, I still get the default error message.

Is there a better way than this ugly hack I'm struggling through? If not, what am I doing wrong in my ugly hack?


In reply to changing P::RD error message from within an action by cazz

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.