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.
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
Outside of code tags, you may need to use entities for some characters:
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.
|
|