zentara has asked for the wisdom of the Perl Monks concerning the following question:
#!/usr/bin/perl use warnings; use strict; use Tk; use Tk::ROText; use IPC::Open3; $|++; my $mw = MainWindow->new; my $canvas = $mw->Canvas( -bg => 'white', -height =>500, -width =>500, )->pack(); my $text = $mw->ROText(-bg=>'white') ->pack( -fill => 'both', -expand => 1 ); $text->tagConfigure( 'red', -foreground => 'red' ); my $pid = open3( \*IN, \*OUT, \*ERR, "/usr/bin/gnuplot" ) || die; $mw->fileevent( \*OUT, readable => \&read_out ); $mw->fileevent( \*ERR, readable => \&read_err ); print IN "set term tkcanvas perltk interactive\n"; print IN "a=1\n"; print IN "set output \'plot.z\'\n"; print IN "plot a\n"; my $gnuplot = do "plot.z"; $gnuplot->($canvas); MainLoop; sub read_out { } sub read_err { print "read_err()\n"; my $num = sysread(ERR, my $buffer, 1024 ); $text->insert( 'end', $buffer, 'red' ); }
Ok, so it's working. But in the next script, I try to avoid having to write out the temp file, and collect the file output into a scalar, and that portion works. But the question is how do I evaluate that scalar so it allows the subs within it to work? The scalar contains a couple of subroutines, with one anonymous one named sub. Any pointers would be appreciated.
#!/usr/bin/perl use warnings; #use strict; use Tk; use Tk::ROText; use IPC::Open3; $|++; my $mw = MainWindow->new; my $canvas = $mw->Canvas( -bg => 'white', -height =>500, -width =>500, )->pack(); my $text = $mw->ROText(-bg=>'white') ->pack( -fill => 'both', -expand => 1 ); $text->tagConfigure( 'red', -foreground => 'red' ); my $pid = open3( \*IN, \*OUT, \*ERR, "/usr/bin/gnuplot" ) || die; $mw->fileevent( \*OUT, readable => \&read_out ); $mw->fileevent( \*ERR, readable => \&read_err ); print IN "set term tkcanvas perltk interactive\n"; print IN "a=1\n"; print IN "plot a\n"; MainLoop; sub read_out { my $sub = ''; # print "read_out()\n"; # my $num = sysread(OUT, my $buffer, 1024 ); my $buffer = <OUT>; print $buffer,"\n"; my $can = $canvas; # my poor attempt to load the subs # my $gnuplot = do \$buffer; # maybe eval $buffer ? # $gnuplot->($canvas); $mw->update; } sub read_err { print "read_err()\n"; my $num = sysread(ERR, my $buffer, 1024 ); $text->insert( 'end', $buffer, 'red' ); }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: subrefs, IPC and gnuplot's tkcanvas
by alpha (Scribe) on Dec 29, 2006 at 16:44 UTC | |
by zentara (Cardinal) on Dec 29, 2006 at 17:54 UTC | |
by alpha (Scribe) on Dec 29, 2006 at 18:12 UTC | |
by zentara (Cardinal) on Dec 29, 2006 at 18:51 UTC |