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

My problem: To produce formatted output in a text widget.

My question: Is it possible to associate a filehandle with a text widget. I'd like to use a FORMAT if possible, but it's not necessary. Having a filehandle, though, would really help things along.

Thanks!

Replies are listed 'Best First'.
Re: Associating filehandles with Perl::Tk widgets?
by pc88mxer (Vicar) on Jul 16, 2008 at 18:36 UTC
    You can access the formatting routines without using a filehandle. See the section "Accessing Formatting Internals" in perldoc perlform.

    Another option is to use a file handle that is tied to a scalar variable, format the text using that file handle and retrieve the formatted output via the scalar:

    my $buf; open(my $fh, ">", \$buf); ... write $fh ...; ... close($fh); # now use $buf: $widget->configure(-text => $buf);

      Okay, that's awesome! I used the first option, and that takes care of a couple of my problems! It's really nice, because I don't have to worry about newlines, either. The only thing is I get the following warning: Use of uninitialized value in formline at ./sl-brad.pl line 375. Here's my code:

      sub swrite { croak "Usage: swrite PICTURE ARGS" unless @_; my $format = shift; $^A = ""; formline( $format, @_ ); return $^A; } #end sub swrite()

      I know it's just a warning, but I'm looking for clean code. I assume it's not liking the @_ in the formline statement. Perhaps I should just assign it to another variable?

        Problem solved. Turned out the data I was sending to swrite was not quite right. Now it is and all is beautiful :) Thanks for your help!

Re: Associating filehandles with Perl::Tk widgets?
by zentara (Cardinal) on Jul 16, 2008 at 18:57 UTC
    Usually you just open the file, and as you loop thru it, insert each line into the text widget. I just slapped together this FORMAT, which is written to a string, then the string is inserted. What are you trying to do? Tail a file or something? Usually with the text widget, you use it's tags mechanism to format your lines. I think you are approaching the problem the wrong way, to put a format in a Tk Text widget.
    #!/usr/bin/perl my $foo = ''; open FH, '+>', \$foo or die $!; format FH_TOP = WHO COMMAND OUTPUT USER TERMINAL DATE & TIME LOCATION ----------------------------------------------------------- . my @who_output= `who`; foreach my $line (@who_output) { my ($userid,$terminal,$month,$day,$time,$location) = split(/\s+/,$line +); chomp $location; format FH = @<<<<<<< @<<<<<<<<< @<<< @<< @<<<<< @<<<<<<<<<<<< $userid $terminal $month $day $time $location . write FH; } #print "$foo\n"; use Tk; my $top = new MainWindow; my $txt = $top->Scrolled("Text")->pack; $txt->insert('end', "$foo\n") ; MainLoop;
    but here is a way to tie stdout to the text widget, but usually you would setup a fileevent ( select ) on a filehandle, then in the fileevent's callback, you insert into the text widget, as you read them.
    #!/usr/bin/perl use warnings; use strict; use Tk; my $mw = MainWindow->new(); my $tx = $mw->Text()->pack(); tie *STDOUT, 'Tk::Text', $tx; $mw->repeat(1000, \&tick); MainLoop; my $count; sub tick { ++$count; print "$count\n"; }

    I'm not really a human, but I play one on earth CandyGram for Mongo