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);
| [reply] [d/l] [select] |
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? | [reply] [d/l] [select] |
| [reply] [d/l] |
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";
}
| [reply] [d/l] [select] |