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

I got some code that looks exactly like this:

my $dbps = DBI->connect($dsn, 'root', 'password'); my $pgprep = $dbps->prepare('SELECT * FROM polls'); $pgprep->execute(); while (my $ref = $pgprep->fetchrow_hashref()) { my @labels = split(/#NEXT#/, $ref->{'choice'}); my @values = split(/#/, $ref->{'votes'}); my @data = (\@labels, \@values); open (TST, ">testtt.txt"); print TST $#values; print TST "\n".$#labels; close (TST); my $graph = new GD::Graph::bars3d(300,300); $graph->set( x_label => 'Poll Choices', y_label => 'Votes', title => $ref->{'title'}, dclrs => [ qw(orange blue green) ], borderclrs => [ qw(orange) ], x_labels_vertical => 1, labelclr => $param{'labelClr'}, axislabelclr => $param{'axisClr'}, textclr => $param{'titleClr'}, cycle_clrs => 1 ); $graph->set_title_font($param{'fontPath'}.$param{'titleFont'}.' +.ttf', $param{'titleSize'}); $graph->set_x_label_font($param{'fontPath'}.$param{'xLabelFont' +}.'.ttf', $param{'xLabelSize'}); $graph->set_x_axis_font($param{'fontPath'}.$param{'xAxisFont'}. +'.ttf', $param{'xAxisSize'}); $graph->set_y_label_font($param{'fontPath'}.$param{'yLabelFont' +}.'.ttf', $param{'yLabelSize'}); $graph->set_y_axis_font($param{'fontPath'}.$param{'yAxisFont'}. +'.ttf', $param{'yAxisSize'}); open (PNG, '>'.$param{'apacheRoot'}.$param{'graphImg'}.$ref->{' +id'}.'.png'); binmode PNG; print PNG $graph->plot(\@data)->png; #THis is where the eval e +rror is. close (PNG); print '<br>&nbsp;'; print '<table cellpadding=2 cellspacing=0 align="center" style= +"border: 0px; width: 88%;">'; print '<tr><td style="color: #3FBFE9; font-weight: bold;" style +="background-image: url(/jpg/layout/redback.jpg);" align="center">'.$ +ref->{'title'}.'</td></tr>'; print '<tr><td style="border: 1px solid #810306;" align="center +">'; print '<img src="'.$param{'absURI'}.$ref->{'id'}.'.png">'; print '</td></tr>'; print '</table>'; }


This code works fine when it's in it's own script, but when i put it inside an eval() function it crashes where i print it to the PNG file ($graph->plot(\@data)->png;).

I know the code is sloppy and what not, but I just want to get it to work first. Is this an issue with my code or the eval() function. Thanks for taking your time to help me.

janitored by ybiC: Obscured db root password as courtesy to posting monk

Replies are listed 'Best First'.
Re: eval() issues.
by tachyon (Chancellor) on Jul 15, 2004 at 07:11 UTC

    My advice would be to distill and add fatal error checking everywhere you call GD functions. I would also separate the $GD->plot( \@data )->png into two calls with error checks on each. This eval example works just fine so it is not the eval that is the issue:

    sub graph_vector { require GD; require GD::Graph::bars; my $png = "c:\\temp\\test.png"; my $GD = GD::Graph::bars->new( 300,300 ) or die $GD->error; my $data = GD::Graph::Data->new( [ [1..26],[1..26] ] ) or die $GD->error; my $gd = $GD->plot( $data ) or die $GD->error; open my $img, '>', $png or die "Couldn't open $png: $!"; binmode $img; print $img $gd->png or die "Error printing png: $!:", $GD->error; close $img; undef $gd; die "Got error!\n"; return 0; } eval{ graph_vector() }; print $@ if $@;

    However if you pass null array refs to new() it chokes. I am calling GD::Graph::Data so you can see that error. I suspect you may not be getting the data from your DB.

    cheers

    tachyon

Re: eval() issues.
by mifflin (Curate) on Jul 15, 2004 at 05:36 UTC
    How do you know that you successfully opened PNG?
    Check to see if you opened the file before you try and write to it.
    The typical thing to do is
    open FH, ">somefile" || die "Could not open somefile : $!";
    On another note. Make sure you check for errors on all your DBI calls (execute, prepare, connect, fetch...). It is better if you open your DBI database handle to throw errors so they can be caught by an exception handler (eval). You do this in the 4th arg of DBI's connect. Like ...
    $dbh = DBI->connect('dbi:DBDnamehere:connectstringhere','userid', ' +password', {RaiseError => 1});
Re: eval() issues.
by matija (Priest) on Jul 15, 2004 at 07:12 UTC
    That is a big, BIG chunk of code. Why do you want to put it into an eval? Why not just change it into a subroutine, and call it?

    If you're changing it dynamicaly in your eval, are you sure that what is getting executed is actualy what you expect?

    Have you tried stepping through the code in a debugger while it executes? Did you look what is happening step by step and check the results of each statement to see if it's getting what you expect? Because I bet the first problem happens way before that print statement.

Re: eval() issues.
by Mercio (Scribe) on Jul 15, 2004 at 05:59 UTC
    Yes, the file is being opened and DBI is recieving no errors. It claims there is an error though. It says this, exactly:

    Can't call method "png" on an undefined value at (eval 7) line 85.

    Line 85 is the line with print PNG $graph->plot(\@data)->png; so I am assuming GD::Graph::bars3d is not plotting the graph. Why? I do not know because it works fine outside of the eval() function. I have been working on this for the past 5 hours and it's driving me insane.
Re: eval() issues.
by Mercio (Scribe) on Jul 15, 2004 at 08:23 UTC
    I fixed it, it was a problem with the references, but it works fine now. Thanks for all your help. It made me look harder.