Help - Search - Member List - Calendar
Full Version: perl and gnuplot
WorkTheWeb Forums > Webmaster Resources > Perl Beginner Help
Support our Sponsors!
Gary Parker
I am attempting to write a perl script to allow users to plot functions using
gnuplot through a web page. It seems to work fine unless the function has any
additions in it. For example, "x+1" plots as just "x" and "x**2+x" won't plot at
all, in fact nothing with "+x" will plot.

The apache error log has not been helpful to me. I can post it if anyone thinks
it will help here.

I have tried running gnuplot in both batch and interactive modes.

A version of the script is availalbe at
http://arithmometer.org/gnuplot/gnuplot_1.pl

Any suggestions would be greatly appreciated.

Gary Parker
Cayuse, OR

Gary Parker
I am guessing that the problem has something to do with the fact that spaces in
the URL using the GET method are represented by addition symbols. I still
cannot find a way around this problem. However, I did try writing the gnuplot
commands to a temporary file and then haing gnuplot retrieve them from the file.
This seems to work fine. Previously, I had fed gnuplot the commands directly.
Any thoughts why one of these methods works and not the other?

Zentara
On Sun, 19 Jun 2005 08:24:39 +0000 (UTC), [Email Removed]
(Gary Parker) wrote:

QUOTE
I am guessing that the problem has something to do with the fact that spaces in
the URL using the GET method are represented by addition symbols.  I still
cannot find a way around this problem.  However, I did try writing the gnuplot
commands to a temporary file and then haing gnuplot retrieve them from the file.
This seems to work fine.  Previously, I had fed gnuplot the commands directly.
Any thoughts why one of these methods works and not the other?

I think you need to look at HTML::Entities, but you
havn't shown your code. You need to properly "decode"
the get string.



--
I'm not really a human, but I play one on earth.
http://zentara.net/japh.html

Gary Parker
zentara <zentara <at> highstream.net> writes:
QUOTE
I think you need to look at  HTML::Entities, but you
havn't shown your code.  You need to properly "decode"
the get string.

Thanks for the tip; I'll check it out. The code is below.

<-- Begin perl script gnuplt_1.pl -->
#!/usr/bin/perl -w

# gnuplot_1.pl
# this script displays a png image generated by gnuplot
# the function plotted is provided by user input through
# an html form (the form is also generatedby this script)
# gnuplot's output is sent directly to the browser through
# an image tag

use strict;
use CGI qw(:standard);
use CGI::Pretty qw( :html3 );
use CGI::Carp qw(fatalsToBrowser);

# define some variables
my ( $function, $domain, $range ) =
( param('function'), param('domain'), param('range') );

unless ($function) { $function = "x"; }
unless ($domain) { $domain = "[-10:10]"; }

# begin script processing
my $choice = lc( param("submit") ); # get choice, lowercased

# determine action from submission choice
if ( $choice eq "" )
{
display_form();
}
elsif ( $choice eq "continue" )
{
display_form();
}
elsif ( $choice eq "plot" )
{
plot();
}
else
{
print header ();
print p ( escapeHTML("Logic error, unknown choice: $choice n") ),
end_html();
}

# Display the form and sample function
sub display_form
{
print header ();

print h1 ( { align => "center" }, "n" ),

start_html(
-title => "Graph with Gnuplot",
-bgcolor => "white",
-style => { 'src' => '../arith/style1.css' }
);

print blockquote (
p("Graphing with Gnuplot"),
p("Enter your function below."),
start_form( -action => url(), -method => "GET" ),

# alternate, call separate program
# "<img src="plot.pl", align="center">",
p(
"<img
src="gnuplot_1.pl?submit=plot&function=$function&domain=$domain&range=$range"
align="center">"
),
table(
Tr(
td("Function:"),
td(
textfield(
-name => "function",
-size => 10,
-maxlength => 30,
-value => "x"
)
)
),
Tr(
td("Domain:"),
td(
textfield(
-name => "domain",
-size => 10,
-maxlength => 15,
-value => "[-10:10]"
),
" (optional)"
)
),
Tr(
td("Range:"),
td(
textfield(
-name => "range",
-size => 10,
-maxlength => 15
),
" (optional)"
)
)
),
submit( -name => "submit", -value => "continue" )
),
end_form(), end_html();
}

# call gnuplot
sub plot
{
print header("image/PNG");

open( PLOT, "| /usr/bin/gnuplot" )
or die "impossible to launch gnuplotn";
print PLOT <<gnuplot_Commands_Done;
set terminal png
set title "Here is your graph."
set nokey
set xtics axis
set ytics axis
set zeroaxis
plot $domain $range $function
gnuplot_Commands_Done
close(PLOT)
or die "impossible to close the pipe -> gnuplot";
}

Zentara
On Sun, 19 Jun 2005 15:34:32 +0000 (UTC), [Email Removed]
(Gary Parker) wrote:

QUOTE
zentara <zentara <at> highstream.net> writes:
I think you need to look at  HTML::Entities, but you
havn't shown your code.  You need to properly "decode"
the get string.

Thanks for the tip; I'll check it out.  The code is below.

Yeah, I looked at your code, it is hard to say where the glitch is.
The $function seems to come through OK, when I print it out
while accessing the script. But you try to "get slick" and resend
the $function back to the script to then feed it to gnuplot. You need
to "re-encode" that get string in that "img src" line below.

Notice in the browser url window, when you submit
http://localhost/~zentara/cgi-bin/gnuplot_...submit=continue

well, that is how your "img src" line below needs to be encoded to.
Maybe you could save the "raw encoded get string" when it comes in,
to avoid having to re-encode it?

I get apache reporting errors in your line
p("<img
src="gnuplot_1.pl?submit=plot&function=$function&domain=$domain&range=$range"

Why don't you try writing the gnuplot output to a temporary file, and
send that file, then delete it. That way you can get it to work for
sure.

There are too many "layers of complexity" to easily spot the problem.

I have seen those problems before myself, and was only able to solve it
by randomly trying different quoting until it worked. :-)
Also, I don't use CGI to output html, so I'm not the right person
to debug your blockquote problem.

Maybe try a version where you manually output the html, like in
"here docs", and avoid the extra layer of complication which CGI's
html output throws at you.

Sorry but I'll leave that long job up to you. :-)

QUOTE
print blockquote (
p("Graphing with Gnuplot"),
p("Enter your function below."),
start_form( -action => url(), -method => "GET" ),

# alternate, call separate program
#        "<img src="plot.pl", align="center">",
p(
"<img
src="gnuplot_1.pl?submit=plot&function=$function&domain=$domain&range=$range"
align="center">"
),
table(



--
I'm not really a human, but I play one on earth.
http://zentara.net/japh.html

Gary Parker
zentara <zentara <at> highstream.net> writes:
QUOTE
Why don't you try writing the gnuplot output to a temporary file, and
send that file, then delete it.  That way you can get it to work for
sure.

I still haven't figured out the problem with the plus symbols. However, I did
find a way around the problem by writing the gnuplot commands to a temporary
file and then having gnuplot retrieve the commands from the file in batch mode.

QUOTE
Maybe try a version where you manually output the html, like in
"here docs", and avoid the extra layer of complication which CGI's
html output throws at you.

I do think my problem is with CGI, though not with the html it generates. It
might be with the way that param deals witht he form data. Also, the error
message from apache had to do with a comment I had in the blockquote CGI
function; with that removed there are no more error messages from apache.

Anyway, I have a functioning verion of the script at:
http://arithmometer.org/gnuplot/gnuplot_4.pl

Thanks again for your assistance.

Gary Parker

Zentara
On Tue, 28 Jun 2005 07:15:08 +0000 (UTC), [Email Removed]
(Gary Parker) wrote:

QUOTE
zentara <zentara <at> highstream.net> writes:
Why don't you try writing the gnuplot output to a temporary file, and
send that file, then delete it.  That way you can get it to work for
sure.

I still haven't figured out the problem with the plus symbols. However, I did
find a way around the problem by writing the gnuplot commands to a temporary
file and then having gnuplot retrieve the commands from the file in batch mode.

Maybe try a version where you manually output the html, like in
"here docs", and avoid the extra layer of complication which CGI's
html output throws at you.

I do think my problem is with CGI, though not with the html it generates.  It
might be with the way that param deals witht he form data.  Also, the error
message from apache had to do with a comment I had in the blockquote CGI
function; with that removed there are no more error messages from apache.

Anyway, I have a functioning verion of the script at:
http://arithmometer.org/gnuplot/gnuplot_4.pl

Thanks again for your assistance.

Gary Parker

Glad you have some success.
Computers are fast, but they sure are "dumb". Just one tiny
little apostophe in the wrong place, and it won't work.

If you really wanted to track down your + problem, the best thing to do
would be to log your network traffic with some tool like
LiveHttpHeaders
Etherreal
or I like tcpick ( search for it on http://freshmeat.net)

Then you can run your various scripts, and compare the working versus
non-working get-strings. Since the + is used in encoding get-strings,
you probably need it encoded (or escaped) somehow.

It can be real grunt work, sometimes even the has to be escaped
with a
I've done it before, and it involved a couple of hours of "trial and
error", until it finally "clicked".




--
I'm not really a human, but I play one on earth.
http://zentara.net/japh.html


PHP Help | Linux Help | Web Hosting | Reseller Hosting | SSL Hosting
This is a "lo-fi" version of our main content. To view the full version with more information, formatting and images, please click here.
Invision Power Board © 2001-2005 Invision Power Services, Inc.