John M. Dlugosz has asked for the wisdom of the Perl Monks concerning the following question:

I'm using a simple tie (below) to have output of commands I run go to a text widget, which is one of the panes in my window.

I'm revisiting this program, and I wonder if now there is something in Perl Tk that does this already, or a more comprehensive module that implements this?

# Tie a file handle to a text widget package OutHandle; use Tie::Handle; use vars qw (@ISA); @ISA= ('Tie::Handle'); sub TIEHANDLE { my ($class, $widget)= @_; my $object= \$widget; return bless \$object, $class; #ref to scalar pointing to widget } sub WRITE { my ($this, $s, $len, $offset)= @_; $s= substr ($s, $offset, $len); if ($s eq "\x19") { #End Of Medium control code used to clear screen $$$this->delete ("1.0", 'end'); } else { $$$this->insert ('end', $s); } $$$this -> update; }
—John

Replies are listed 'Best First'.
Re: Is there a Perl Tk "console" widget?
by Rich36 (Chaplain) on Nov 21, 2002 at 21:05 UTC

    There's a simpler way in Tk to do the tie. You specify the filehandle, the widget type and the variable representing the widget. Also, this might be something else worth looking into.

    # Tie STDOUT to the ROText box so that the messages to STDOUT appear t +here tie (*STDOUT, 'Tk::ROText', $widget);

    «Rich36»
      Hmm, that works...eventually. It doesn't have the equivilent of my $$$this->update on each call to WRITE, so no output shows up until the whole thing is done.

      The tie ability is not mentioned in the Tk::Text or Tk::ROText docs. Where is that discussed? Maybe there's information there about configuring it, too.

      —John Update: I found the PRINT method in Tk::Text.pm and added a call $w->update;. That did the trick. There is no configuration ability in that code.