Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options

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

by vrk (Chaplain)
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";

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://710844]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (4)
As of 2018-02-25 13:58 GMT
Find Nodes?
    Voting Booth?
    When it is dark outside I am happiest to see ...

    Results (312 votes). Check out past polls.