SSilk SSilk - 1 year ago 154
Perl Question

Perl Chart::Gnuplot - access ASCII output of plot2d

I'm using the Perl module Chart::Gnuplot to produce some ASCII plots with

terminal 'dumb'

Instead of having the function plot2d directly print the plot results I would like to capture the nicely formatted ASCII plot in a temporary variable so I can print it later.

Here is a MWE of what I'm doing, showing what works and what does not.

use strict;
use warnings;
use Chart::Gnuplot;

my @xvals=(1...10);
my @yvals=(1...10);

my $chart = Chart::Gnuplot->new(
xrange => [0, 10],
yrange => [0, 10],

my $dataset = Chart::Gnuplot::DataSet->new(
style => "dots",
xdata => \@xvals,
ydata => \@yvals


$chart->plot2d($dataset); # This works

my $textplot = $chart->plot2d($dataset); # This does not work, nor did I expect it to
print($textplot); # Prints a hash ref like "Chart::Gnuplot=HASH(0x6000cbb80)"

As I mentioned, I didn't expect plot2d to directly output the plot, but I'm wondering if there is some method/function/whatever that would allow me to do this. Can I tell plot2d to plot to a string variable instead of STDOUT?


Answer Source

Create a temporary file (say using File::Temp) and pass its path to the constructor's output option. You can collect the output from that temporary file.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download