I tried redirecting STDERR to a variable or a file, but this does not take effect inside P::RD because of the way the STDERR is handled. I'd prefer not to meddle with P::RD, but if that's the only solution then I will. Here is some sample code which illustrates my point. The "This is an error message\n" gets read into the variable as expected, but P::RD's error messages are printed to STDERR nonetheless.
Jonas use strict; use Parse::RecDescent; my $grammar = q { <autotree> query : disjunction_phrase_or_identifier end_of_input | <error> disjunction_phrase_or_identifier : conjunction OR disjunction_phrase_or_identifier | conjunction conjunction : word AND(?) conjunction | word word : bracket_expression | phrase | identifier_without_keywords | <error> bracket_expression : '(' disjunction_phrase_or_identifier ')' phrase : '"' identifier(s?) '"' | <error> identifier_without_keywords : ...!/^OR\s/i ...!/^AND\s/i /%?[a-zA-Z\d]+\*?/ identifier : /[a-zA-Z\d]+\*?/ AND : /^AND\s/i OR : /^OR\s/i end_of_input : /^\Z/ }; my $parser = new Parse::RecDescent($grammar); die "Bad grammar - $!" unless $parser; my $stderr; open STDERR, '>', \$stderr; print STDERR "This is an error message\n"; my $result = $parser->query(join ' ', @ARGV); close STDERR; print "Got STDERR: \n$stderr"; 1;