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

I was wondering... suppose I have a filehandle open for output...

Can I have a process listening to what is written to that output?

Say, something close to:

open(FH, STDOUT);

And then, we could receive everything we would print with <FH>...

Can it be done?

Retitled by davido.

Replies are listed 'Best First'.
Re: Possible to have a process monitoring another's stdout asynchronously?
by gellyfish (Monsignor) on Jan 13, 2005 at 12:00 UTC
      I'm not sure what I want to do could be done with any of those...

      As an example, suppose I had STDOUT, wanted to keep it that way (as an output channel) but also wanted to print everything that went to STDOUT to two other files... with one single print.

      I can't see how to do this with IO::Select...

        Perhaps IO::Tee is what you're looking for...

        thor

        Feel the white light, the light within
        Be your own disciple, fan the sparks of will
        For all of us waiting, your kingdom will come

Re: Possible to have a process monitoring another's stdout asynchronously?
by davido (Cardinal) on Jan 13, 2005 at 17:04 UTC

    You can tie the filehandle to a class that adds any amount of behind-the-scenes functionality to the basic filehandle. To the rest of your script, the filehandle would look and behave pretty much like a regular filehandle, but behind the scenes all sorts of stuff can be going on. perltie has info on how to do this, and you can also read more at Tie::Handle.

    This sort of approach isn't for the faint of heart, so be sure to get yourself in good physical and mental shape before trying these stunts. *wink*


    Dave

Re: Possible to have a process monitoring another's stdout asynchronously?
by zentara (Cardinal) on Jan 13, 2005 at 14:04 UTC
    If you want to use Tk, you can use the fileevent method, which will sit there and wait until it sees output.
    #!/usr/bin/perl use warnings; use strict; use Tk; my $mw = new MainWindow; my $t = $mw->Text(-width => 80, -height => 25, -wrap => 'none')->pack( +-expand => 1); open(CHILD, "./some_program 2>&1 |") or die "Can't open: $!"; $mw->fileevent(\*CHILD, 'readable', [\&fill_text_widget,$t]); MainLoop; sub fill_text_widget { my($widget) = @_; $_ = <CHILD>; $widget->insert('end', $_); $widget->yview('end'); }

    I'm not really a human, but I play one on earth. flash japh