in reply to gnuplot and CGI

Thanks to all the responses, I got this working having the image src call a separte cgi program.
<img src="test4.pl?sta=$station&const=$constituent" alt="$station $constituent" border="1">
#!/usr/bin/perl -w # test4.pl for testing real-time graph capabilities # November 9, 2004 use CGI qw( :standard ); use CGI::Carp 'fatalsToBrowser'; my $q = new CGI; my $station = $q->param('sta'); my $constituent = $q->param('const'); my $combined = "$station" . "$constituent"; my $img = `/usr/local/bin/gnuplot $station $constituent $combined`; print $q->header("image/png"), $img;
However, because I need to change the gnuplot set commands based on user input, I want to do this differently. Something like
#!/usr/bin/perl -w # test5.pl for testing real-time graph capabilities # November 17, 2004 use CGI qw( :standard ); use CGI::Carp 'fatalsToBrowser'; #will print error message to user $CGI::POST_MAX = 512; # limit maximum posting size to 512 bytes $CGI::DISABLE_UPLOADS = 1; # disables uploading files entirely my $q = new CGI; my $data = "05056670.txt"; print $q->header("image/png\n\n"); my $img = plotdata(); sub plotdata { open my $graph => "| /usr/local/bin/gnuplot" or die "Failed to open +pipe: $!\n"; print $graph <<"gnu"; set terminal png color set output set xdata time set timefmt "%Y%m%d" set key left top title "Legend" box set grid xtics ytics set yrange [700:] set format x "%Y" set xlabel "Year" set ylabel "Sodium, water, filtered, milligrams per liter" set title "05056670 Western Stump Lake Major Ions" plot "$data" using 2:3 title "P00930 Sodium dissolved" gnu close $graph or die "Failed to close pipe: $!\n"; }

When I try this, I get:
PNG

IHDRsBITLˢ0PLTEUUUUUUUUUUUUIN.tEXtSoftwaregnuplot Unix version 3.7 patchlevel 1CIDATxKң`3&5e'5eHJ@ !tt ?i    Cۗn4oam]$붛lϐ>a~of&w0߇Nܺ aٍۇ):M}ݍ:MQuaJn>uF}zYO/$$Gg\5l=oOe-LfeƧ’.&<)9դ1mt6v0QfN.T#F}!YuSԏzcn@7(Mr;


in the browser.

What am I doing wrong?