Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW

Comment on

( #3333=superdoc: print w/ replies, xml ) Need Help??

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 ERROR (line 1): Invalid start: Was expecting /foo/

Now, if you change require at line 13 to use,

$ perl ERROR (line 1): Invalid start: Was expecting /foo/ Died at 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";


$ perl Undefined format "Parse::RecDescent::ERROR" called at /usr/share/perl5 +/Parse/ 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";

In reply to Re^2: How to grab Parse::RecDescent error output in a variable? by w-ber
in thread How to grab Parse::RecDescent error output in a variable? by w-ber

Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":

  • 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
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?

    What's my password?
    Create A New User
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others taking refuge in the Monastery: (5)
    As of 2015-10-10 18:32 GMT
    Find Nodes?
      Voting Booth?

      Does Humor Belong in Programming?

      Results (257 votes), past polls