ruscoekm has asked for the wisdom of the Perl Monks concerning the following question:

This question arose from a previous one related to filtering the output of Parse::RecDescent:

http://www.perlmonks.com/index.pl?node_id=209943

I thought I would reproduce it in a new thread, as the old one is now rather buried in the chain.

In the example code below, the prints come through fine but the writes do not. It seems as though writes to a format cannot be tied:

use strict; use warnings; package Log::OPG::Info; use Tie::Handle; use base qw(Tie::StdHandle); sub WRITE { print "Good\n"; } package main; format STDERR = Bad . tie *STDERR, 'Log::OPG::Info'; print STDERR ''; write STDERR;

Whereas I hope for the output:

Good Good

I actually get:

Good Bad

So, either I am missing something obvious, or the question is:

"How does one tie writes to a format?"

Cheers
Kevin

P.S. I faintly remember that it is possible to name individual slots in hash table entries, along the lines of $::slot{SCALAR} and $::slot{FORMAT}. Perhaps that might form the basis for a solution, but the details escape me.

Replies are listed 'Best First'.
•Re: Tie'ing writes to formats
by merlyn (Sage) on Nov 02, 2002 at 15:08 UTC
    According to the docs, WRITE is for syswrite, not write. You should try providing the PRINT or PRINTF tie routines and see if the write eventually triggers down through there.

    -- Randal L. Schwartz, Perl hacker
    Be sure to read my standard disclaimer if this is a reply.

      Hi Randal

      I did try providing PRINT and PRINTF. More to the point, I checked the code of Tie::Handle and Tie::StdHandle first. (Log::OPG::Info inherits from Tie::StdHandle, which itself inherits from Tie::Handle.) Tie::Handle provides PRINT and PRINTF methods, which simply invoke WRITE, if such a method has been defined apart from the one in Tie::Handle itself.

      Also, from stepping through the code in the debugger, it appears as though the write function does not step into any of the Tie:: methods at all.

      Cheers
      Kevin
      
Re: Tie'ing writes to formats
by runrig (Abbot) on Nov 02, 2002 at 17:53 UTC
    "How does one tie writes to a format?"

    You can't. Though through use of $^A and formline you can get format like output in a scalar, and from there you can print it to whatever file handle you like.

      Thanks, suspected as much :-(

      The problem here is that it is P::RD which is writing to the format, so it is difficult to capture the output. (see Re: Re: Filtering my own stderr).

      Cheers, Kevin

Re: Tie'ing writes to formats
by broquaint (Abbot) on Nov 02, 2002 at 18:18 UTC
    I faintly remember that it is possible to name individual slots in hash table entries, along the lines of $::slot{SCALAR} and $::slot{FORMAT}.
    While you can access individual slots in globs to get access a given datatype, you can't get a FORMAT slot.
    HTH

    _________
    broquaint

      Thanks. It is beginning to sound as though there is no way (hesitates to say this, as there is always a way) I can trap the format output of P::RD (which is opened to a dup(2)ed STDERR) short of doing something extreme like trying to forking a child to poll for output on all of the parent's filehandles.