in reply to save a png image

As someone else pointed out, you should check for errors. Not only with $graph->plot but also with open, binmode, print and close. These all return undef on failure and have the error in $!.

open TMPFILE, ">file.png" or die $!; binmode TMPFILE or die $!; print TMPFILE $db_image->png or die $!; close TMPFILE or die $!;

This is the bare minimum of error checking and reporting that you should do. It is perhaps a good idea to make full sentences: "Could not open file.png: $!\n".

Alternatively, you can use the Fatal module to wrape open, binmode and close:

use Fatal qw(open binmode close); open my $tmpfile, ">file.png"; binmode $tmpfile; print $tmpfile $db_image->png or die $!; close $tmpfile;
Note that I used a lexical filehandle here. Globals are bad for many reasons. See Coping with scoping for information about that.

When you have done all this, it's time to take indentation lessons :) In fact, you should probably just get a good Perl book or tutorial to learn from. (Like Beginning Perl or this tutorial or one of the many non-free resources out there.)

Juerd # { site => 'juerd.nl', plp_site => 'plp.juerd.nl', do_not_use => 'spamtrap' }

Replies are listed 'Best First'.
Re: Re: save a png image
by Anonymous Monk on Mar 09, 2004 at 17:20 UTC
    sorry forgive my indentation and untidyness but i cracking up ere about this and it driving me nuts
    my $gd_image = $graph->plot(\@data) or die $graph->error; print header(-type => "image/png") or die $!; binmode STDOUT or die $!; print $gd_image->png or die $!;
    i would give up on the save to file thing if i could get this code to display multiple images on the one screen i doing a project similar to mrtg for collage and i want to be able to display multiple graphs as they do.. using the above code i get no errors but i only get shown the first graph