1

考虑这个例子:

#!/usr/bin/perl -w
use strict;
use Chart::Gnuplot;

# Initiate the chart object
my $chart = Chart::Gnuplot->new(
    output => "plotStyle_1.png",
);

# A line
my $lines = Chart::Gnuplot::DataSet->new(
    func  => "cos(x)",
    style => "lines",
    title => "Plot a line",
);

# Points
my $points = Chart::Gnuplot::DataSet->new(
    func  => "sin(x)",
    style => "points",
    title => "Plot points",
);

# Points on a line
my $linespoints = Chart::Gnuplot::DataSet->new(
    func  => "-atan(x)",
    style => "linespoints",
    title => "Plot points on a line",
);

# Plot the graph
$chart->plot2d($lines, $points, $linespoints);

我想要的是将 plot2d 中的 3 个对象(代码中的最后一行)推送到一个数组并以这种方式调用它:

$chart->plot2d(@array);

这是plot2d的实现

    sub plot2d
{
    my ($self, @dataSet) = @_;
    &_setChart($self, \@dataSet);

    my $plotString = join(', ', map {$_->_thaw($self)} @dataSet);
    open(GPH, ">>$self->{_script}") || confess("Can't write $self->{_script}");
    print GPH "\nplot $plotString\n";
    close(GPH);

    # Generate image file
    &execute($self);
    return($self);
}

*可以用数组作为参数调用它吗?* 请注意,我不是 perl 专家,我无法理解 plot2d 的实现

4

1 回答 1

2

是的,这是可能的,就去做吧。在您的具体情况下,这将是:

my @datasets;
push @datasets, $lines;
push @datasets, $points;
push @datasets, $linespoints;

$chart->plot2d(@datasets);
于 2013-09-21T08:38:58.657 回答