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

Last week, with the help of perlmonks, I got the following code to work outputting an image to the browser (another cgi page references this code as the source of the image).
#!/usr/bin/perl -w # test.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;
Now I want to do the process differently to more easily adjust the gnuplot set commands based on user input. I've been trying variations of the following code, but I'm getting either strange characters printed to the browser or currently the error "The server encountered an internal error and was unable to complete your request. Error message: Premature end of script headers: test6.pl"
#!/usr/bin/perl -w # test6.pl for testing real-time graph capabilities # November 17, 2004 use CGI qw( :standard ); use CGI::Carp 'fatalsToBrowser'; #will print error message to user my $q = new CGI; open $station => "| /usr/local/bin/gnuplot" or die "$!\n"; print $station <<"--"; 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 "05056670.txt" using 2:3 title "P00930 Sodium dissolved" -- print $q->header("image/png"),$station;

Any suggestions as to what I am doing wrong?

Thanks

Replies are listed 'Best First'.
Re: gnuplot output to browser
by Joost (Canon) on Nov 17, 2004 at 19:55 UTC
      Thank you! I fixed that and now have
      #!/usr/bin/perl -w # test6.pl for testing real-time graph capabilities # November 17, 2004 use CGI qw( :standard ); use CGI::Carp 'fatalsToBrowser'; #will print error message to user my $q = new CGI; print $q->header("image/png"); open $station => "| /usr/local/bin/gnuplot" or die "$!\n"; print $station <<"--"; 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 "05056670.txt" using 2:3 title "P00930 Sodium dissolved" --
      I'm still getting Server error! The server encountered an internal error and was unable to complete your request. Error message: Premature end of script headers: test6.pl

      Any other suggestions?
Re: gnuplot output to browser
by traveler (Parson) on Nov 17, 2004 at 22:10 UTC
    It appears that in the first example you print $img which is the output of the gnuplot command. In the second you are trying to print the filehandle $station.

    You could choose to put the commands in a file and capture the output (as you did in the first case) or do the commands in the program and capture the output (with >somefile in the open) or you could print the header with your script, provide the plot commands in your script and let gnuplot write to the standard output.

    HTH, --traveler