Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

Problems with references using GD::Graph::Data

by gary kuipers (Beadle)
on Apr 01, 2002 at 18:18 UTC ( [id://155812]=perlquestion: print w/replies, xml ) Need Help??

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

I am doing something improperly with references, but I can't see it! This is the error I get.

Can't call method "gif" on an undefined value at test2.pl line 80.
That line is 3rd from the bottom on the snippet below.
this is the piece of code that generates the above error:
my $data = GD::Graph::Data->new(); my $graph = GD::Graph::bars->new(); my $format = $graph->export_format; if ( $dbh = DBI->connect("DBI:ODBC:$INI{DSN}",$INI{DBuser},$INI{DBp +ass}) ) { my $gotdata = 0; my $stmt = 'SELECT SysID FROM MeterEvent WHERE Sysid >= 2 and Sy +sid < = 10'; if ( my $sth = $dbh->prepare($stmt) ) { if ( my $rows = $sth->execute ) { while (my @row = $sth->fetchrow_array) { $data->add_point(@row); $gotdata = 1; } } } $dbh->disconnect(); } else { print "Database Error$br\n"; } my $gd = $graph->plot($data); open(IMG, '>file.gif') or die $!; binmode IMG; print IMG $gd->gif; # this is the line # 80 if ( defined $gd ) { print " 4.gd defined\n"; } else {print " 4.gd +NOT defined\n"; } close IMG;
The GD::Graph::Data manual states the following in the examples section
use GD::Graph::Data; use GD::Graph::bars; my $data = GD::Graph::Data->new(); $data->read(file => '/data/sales.dat', delimiter => ','); $data = $data->copy(wanted => [2, 4, 5]); # Add the newer figures from the database use DBI; # do DBI things, like connecting to the database, statement # preparation and execution while (@row = $sth->fetchrow_array) { $data->add_point(@row); } my $chart = GD::Graph::bars->new(); my $gd = $chart->plot($data);
Thanks for your help!

Replies are listed 'Best First'.
Re: Problems with references using GD::Graph::Data
by buckaduck (Chaplain) on Apr 01, 2002 at 18:56 UTC
    Presumably, $gd is undefined. I'm guessing that the GD::Graph::bars module is unhappy with the fact that you're trying to create a graph out of a one-dimensional set of data.

    At least your data looks one-dimensional to me, based on the SQL query that you're using. You're adding a series of X values with no associated Y values. What sort of graph are you hoping to produce from this? Now if you included some Y values, maybe things would be different...

    buckaduck

      You are right! When I use X and Y values ( by saying SELECT SysID, SysID FROM ..." the graph prints without problems. I made a bad assumption ... that the module would provide a sequential X value. Not so! Anyway, thanks!
Re: Problems with references using GD::Graph::Data
by metadoktor (Hermit) on Apr 01, 2002 at 18:49 UTC
    I assume that you're using warnings and use strict. Are you using?
    use diagnostics

    Also perhaps you might want to rearrange some of your code so that:

    print IMG $gd->gif; if (defined $gd)
    becomes
    if (defined $gd) { print IMG $gd->gif; } else { ... }
    conditional.

    metadoktor

    "The doktor is in."

Re: Problems with references using GD::Graph::Data
by mla (Beadle) on Apr 01, 2002 at 20:37 UTC
    You need to add more error checking. Often the man pages are very terse and skip it, but it becomes really valuable once you begin writing code of any significant length.

    In this case, a quick look at the GD::Graph::Data POD suggests that calling the error() method might tell you what went wrong.

    Lots of additional error info is discussed in the GD::Graph::Error POD, including the $GD::Graph::Error::Debug variable which might also give you a better hint.

    And use RaiseError on your DBI handle! See the DBI docs if you aren't familiar with it. It makes your error handling both simpler and more robust since you don't have to add error checks, and you don't have to remember to add error checks.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://155812]
Approved by root
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others avoiding work at the Monastery: (7)
As of 2024-04-23 20:14 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found