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' }


In reply to Re: save a png image by Juerd
in thread save a png image by Anonymous Monk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.