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

Good day monks. I have a script with two subroutines that use GD and GD::Graph::lines to create graphics and write them to separate files. I am trying to refactor the code so the subroutines will return the GD objects instead, so I can comdine them into one graphic with GD's copy method and write a single file. However, I'm doing something wrong when it comes to passing the GD image object for the graph. Here is some simplified code that reproduces the problem
#!/usr/bin/perl -w use strict; use GD; use GD::Graph::lines; my $g = new GD::Image(400,300); plot($g); my $a = new GD::Image(20,300); addon($a); my $combined = new GD::Image(420,300); $combined->copy($g,0,0,0,0,400,300); $combined->copy($a,400,0,0,0,20,300); open(OUT,">combined.png") or die "Can't open output: $!\n"; binmode OUT; print OUT $combined->png; close OUT; sub plot { my ($graphimage) = @_; my @data = ( ["1st","2nd","3rd","4th","5th","6th","7th", "8th", "9th"], [ 1, 2, 5, 6, 3, 1.5, 1, 3, 4] ); my $graph = GD::Graph::lines->new(400, 300); $graph->set( x_label => 'X Label', y_label => 'Y label', title => 'Some simple graph', y_max_value => 8, y_tick_number => 8, y_label_skip => 2 ) or die $graph->error; $graphimage = $graph->plot(\@data) or die $graph->error; open(OUT,">combined-plotpart.png") or die "Can't open output: $!\n +"; binmode OUT; print OUT $graphimage->png; close OUT; return; } sub addon { my ($gdadd) = @_; my $blue = $gdadd->colorAllocate(0,0,255); $gdadd->rectangle(0,0,20,300,$blue); return; }
When this is run, combined.png just has a big black square where the plot should be.

I'm baffled by this because when I write out the graphic in the plot subroutine (combined-plotpart.png) it has the correct graph, and I am passing it through a reference to the object just like for the addon image, which is working OK.

What am I doing wrong?

TIA....Steve

Replies are listed 'Best First'.
Re: Problem passing GD image object
by GrandFather (Saint) on Mar 18, 2007 at 00:19 UTC

    Your immediate problem is that the line:

    $graphimage = $graph->plot(\@data) or die $graph->error;

    in sub plot replaces the object you passed in. The easiest way to handle the issue is to simply return the object from plot. You don't need to pass anything in:

    my $g = plot (); ... sub plot { my @data = ( ... ) or die $graph->error; return $graph->plot(\@data) or die $graph->error; }

    DWIM is Perl's answer to Gödel
      You're right, that fixed it. Thanks.

      I still don't understand, tho. Even if I replaced the object, it seems like the replaced object should have worked anyway.

        You would need to pass a reference to the object then update the referenced value:

        plot (\$g); ... $$graphimage = $graph->plot(\@data) or die $graph->error;

        DWIM is Perl's answer to Gödel