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

In reply to subrefs, IPC and gnuplot's tkcanvas by zentara

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.