I am trying to graph a temp file. I am using GD::Graph:
ata to create the graph. Here is some of my code.
I get an error that says "Can't call method 'fetchrow_array' on an undefined value". I checked my temp file and it does in fact contain data. What could be causing this?
Code:
open(FILE, "<timedata.txt") or return; # Open file
my $fh = IO::File->new_tmpfile or die "Unable to make new temp file: $!";
$fh->autoflush(1);
my $headercount = 1;
my $discard = <FILE> for 1 .. $headercount;
foreach $line (<FILE>) {
chomp $line;;
($timestamp, undef, undef, $out, undef, undef, undef, $in, undef)=split(/\t/, $line);
my $datetime = DateTime::Format::Excel->parse_datetime($timestamp);
$timestamp = $datetime->ymd . $datetime->hms;
print $fh "$timestamp $out $in\t\n";
seek ($fh, 0, 0) or die "Seek: $!";
}
close (FILE);
# Create graph of the file
my $datafile = GD::Graph::Data->new();
$datafile->read(file => ‘$fh’, delimiter => '\t');
$datafile = $datafile -> copy(wanted => [0, 1, 2]);
while (my @row = $fh->fetchrow_array()) <-------- This is where I have trouble
{
$datafile ->add_point($row);
}
my $graph = GD::Graph::points->new(400, 300);
$graph->set(
x_label => 'Time/Date',
y_label => 'Count',
title => "Count of Data",
) or warn $graph->error;
my $image = $graph->plot($data) or die $graph->error;
open(GRAPH, ">graph.png") or die "Not able to open image file\n";
binmode GRAPH;
print GRAPH $image->png or die "Problem writing to image file\n";
I get an error that says "Can't call method 'fetchrow_array' on an undefined value". I checked my temp file and it does in fact contain data. What could be causing this?