Oh divine ones, I'm back. Here's what I'm trying to do. Check out this code:
# a frame in my Tk main window my $frame = $mw->Frame; # a text widget with attached scrollbars in my frame my $OutputText = $frame->Scrolled('Text', -height => '50', -width => '150', -scrollbars => 'osoe' ); # pack frame and text widget $frame->pack(qw/-side left -fill y/); $OutputText->pack(qw/-side bottom -fill both -expand 1/); # here I try to tie STDOUT to the text widget my $widget = $OutputText->Subwidget("text"); tie *STDOUT, ref $widget, $widget;
So the idea in the code above is the following: anything printed to STDOUT later on in the program should appear in the text widget. Lo and behold, that works, except for what I assume is a buffering issue.

If I just look at STDOUT, the messages that the program spits out appear on STDOUT as they are printed. Fine. But if I look at my text widget, messages appear only much later (e.g., after we've iterated through a loop). For example, if I do:
for ( 1..10 ) { sleep 10; print "foo\n"; }
I get ten lines of "foo" after 100 seconds on my text widget, while I get a "foo" every 10 seconds on STDOUT.

I assume this is something to do with buffering but I don't quite understand how that works (esp. with respect to tie-ing). I don't think STDOUT is buffered at all? Or line-buffered? Setting STDOUT->autoflush(1) has no effect. What to do?

Thanks for any and all replies!

In reply to Tk::Text and buffering, I think by rvosa

Title:
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!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • 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, details, 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, summary, 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.