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

Hi, I'm trying to run the latest gnuplot thru it's tk interface. According to the docs, it asks you to write out a file, called (for instance) plot.z, then read that file in with "do plot.z" This all works as shown in the first script.
#!/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' ); }

I'm not really a human, but I play one on earth. Cogito ergo sum a bum

Replies are listed 'Best First'.
Re: subrefs, IPC and gnuplot's tkcanvas
by alpha (Scribe) on Dec 29, 2006 at 16:44 UTC
    try eval(). basically, do FILE = eval `cat FILE`;
      I tried eval, but get errors like
      my $gnuplot = eval \$buffer->($canvas); #OUTPUT: #Undefined subroutine &main::sub {
      or
      my $gnuplot = eval \$buffer; $gnuplot->($canvas); # OUTPUT # Use of uninitialized value in subroutine entry at .... # Undefined subroutine &main:: called at ./zz-std.....

      I'm not really a human, but I play one on earth. Cogito ergo sum a bum
        eval($buffer), not \$buffer. same with $buffer->($canvas).