in reply to Re: How to grab Parse::RecDescent error output in a variable?
in thread How to grab Parse::RecDescent error output in a variable?
About the use of require in this example... Here is a complete working example that captures Parse::RecDescent output in a dup'd anonymous filehandle, which shows that use and require are not equivalent in this case.
use strict; my $ParseErrorFh; BEGIN { open(my $olderr, '>&STDERR') or die "Cannot dup STDERR: $!"; close STDERR or die "Cannot close STDERR: $!"; open(STDERR, '+>', undef) or die "Cannot open anonymous file: $!"; select STDERR; $| = 1; open($ParseErrorFh, '>&STDERR') or die "Cannot dup anonymous file: + $!"; require Parse::RecDescent; close STDERR or die "Cannot close STDERR: $!"; open(STDERR, '>&', $olderr) or die "Cannot restore STDERR: $!"; } sub parse { my ($grammar, $str) = @_; local $::RD_ERROR = 1; local $::RD_WARN = 2; seek($ParseErrorFh, 0, 0); my $p = Parse::RecDescent->new($grammar) or die "Grammar is invalid"; my $x = $p->start($str); if (not defined $x) { seek($ParseErrorFh, 0, 0); die join '', grep { $_ !~ m/^\s*$/ } <$ParseErrorFh>; } return $x; } print parse('start: /foo/ | <error>', 'fo'), "\n";
Running this you will get the output
$ perl rectest.pl ERROR (line 1): Invalid start: Was expecting /foo/
Now, if you change require at line 13 to use,
$ perl rectest2.pl ERROR (line 1): Invalid start: Was expecting /foo/ Died at recdescent.pl line 34.
Here, the first two lines are printed to STDERR, while the last one indicates that nothing is captured in the dup'd filehandle. Regardless of what the documentation says about use being equivalent to require Module; import Module;, this example shows that it is not the case this time.
Anyway, it would be cleaner to re-open Parse::RecDescent::ERROR.
Yes, it definitely would be. This is what I tried first. Here is a complete example (did you try to run your own example code?):
use strict; use Parse::RecDescent; sub parse { my ($grammar, $str) = @_; open(local *Parse::RecDescent::ERROR, '>', \my $error) or die "Cannot open in-memory filehandle: $!"; local $::RD_ERROR = 1; local $::RD_WARN = 2; my $p = Parse::RecDescent->new($grammar) or die "Grammar is invalid"; my $x = $p->start($str); defined $x or die $error; return $x; } print parse('start: /foo/ | <error>', 'fo'), "\n";
Result:
$ perl recdescent3.pl Undefined format "Parse::RecDescent::ERROR" called at /usr/share/perl5 +/Parse/RecDescent.pm line 2910.
Clearly format has a side-effect that prevents the use of the nice solution this time. Besides that, your re-open of ERROR requires knowledge of the package internals, while redirecting STDERR requires arguably less knowledge, and certainly not the name of a private (albeit package global) variable.
--
say "Just Another Perl Hacker";
|
---|
Replies are listed 'Best First'. | |
---|---|
Re^3: How to grab Parse::RecDescent error output in a variable?
by ikegami (Patriarch) on Sep 15, 2008 at 12:00 UTC | |
Re^3: How to grab Parse::RecDescent error output in a variable?
by tfrayner (Curate) on Sep 15, 2008 at 13:09 UTC | |
by Pic (Scribe) on Sep 15, 2008 at 17:12 UTC | |
by vrk (Chaplain) on Sep 17, 2008 at 08:21 UTC |