Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked

Re: How to grab Parse::RecDescent error output in a variable?

by w-ber (Hermit)
on Sep 12, 2008 at 08:44 UTC ( #710844=note: print w/ replies, xml ) Need Help??

in reply to How to grab Parse::RecDescent error output in a variable?

Answering my own question, here is one way to do it:

my $ParseErrorFh; BEGIN { open(my $olderr, '>&', STDERR) or die "Cannot dup STDERR: $!"; close STDERR; open(STDERR, '+>', undef) or die "Cannot open anonymous file: $!"; select STDERR; $| = 1; open($ParseErrorFh, '>&STDERR') or die "Cannot dup anoynomous file +: $!"; # Cannot use, since that messes up execution order for some reason + (since use # implies a BEGIN block itself?). require Parse::RecDescent; close STDERR; open(STDERR, '>&', $olderr) or die "Cannot restore STDERR: $!"; } sub report_error { seek($ParseErrorFh, 0, 0); die join '', grep { $_ !~ m/^\s*$/ } <$ParseErrorFh>; } sub parse { my ($grammar, $str) = @_; seek($ParseErrorFh, 0, 0); # To ensure it's not growing indefinite +ly, as it is package global. my $p = Parse::RecDescent->new($grammar) or report_error(); seek($ParseErrorFh, 0, 0); return $p->startrule($str) or report_error(); }

Of course, this is not thread-safe, and it is also quite ugly. Other solutions?

say "Just Another Perl Hacker";

Comment on Re: How to grab Parse::RecDescent error output in a variable?
Download Code

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://710844]
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others rifling through the Monastery: (8)
As of 2015-11-30 09:12 GMT
Find Nodes?
    Voting Booth?

    What would be the most significant thing to happen if a rope (or wire) tied the Earth and the Moon together?

    Results (767 votes), past polls