Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

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?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others having an uproarious good time at the Monastery: (3)
As of 2014-08-31 03:19 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The best computer themed movie is:











    Results (294 votes), past polls