FAQ
Hi,

I want to capture error messages issued from the parser using the <error>
directive into a variable instead of echoing them to STDERR. Is there any
way to do this?

Thanks, Jonas

Search Discussions

  • Jonas Wolf at Jul 9, 2004 at 9:03 am
    I just browsed the archives and found this:

    http://www.mail-archive.com/recdescent@perl.org/msg00215.html

    which is the exact question I just asked... Can I assume from the
    unanimous silence that nothing has happened about this in the last two
    years?

    Jonas
  • Alexander Farber at Jul 9, 2004 at 9:44 am
    Hi,

    I think your question is related to the OS you're using and not to P::RD.

    Have you tried redirecting STDERR (as in "perldoc -f open") before calling the parser?

    And you can always modify Descent.pm if you don't like what it's doing with STDERR ;-)

    Regards
    Alex
    -----Original Message-----
    From: ext Jonas Wolf

    http://www.mail-archive.com/recdescent@perl.org/msg00215.html

    which is the exact question I just asked... Can I assume from the
    unanimous silence that nothing has happened about this in the
    last two
    years?
  • Jonas Wolf at Jul 9, 2004 at 10:08 am
    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;
  • Karl Gaissmaier at Jul 16, 2004 at 8:10 am

    Jonas Wolf schrieb:

    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.
    you can cheat P::RD with directly accessing $thisparser->{errors},
    see the FAQ: 'Accessing error data' and the answer by Damian

    Best Redards
    Charly

    --
    Karl Gaissmaier KIZ/Infrastructure, University of Ulm, Germany
    Email:karl.gaissmaier@kiz.uni-ulm.de Service Group Network

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
grouprecdescent @
categoriesperl
postedJul 7, '04 at 9:43a
activeJul 16, '04 at 8:10a
posts5
users3
websitemetacpan.org...

People

Translate

site design / logo © 2018 Grokbase