Presenting Mathematical Concepts on the World Wide Web
Forms & Scripts

Using Scripts: Using Maple and Mathematica

Set the Path

Perl scripts can start other programs, send commands to them, and retrieve their output.

When a CGI script runs, it behaves as a separate user, the http daemon. Consequently, it inherents a user envrionment. Typically, this environment is minimal, containing almost no path information, for example. Thus, if you are going to run other software, you must add the path to the script's environment:

$ENV{PATH} .= ":/usr/local/bin:/u/share/bin";
The ".=" operator appends the string to the existing path. The associative array %ENV contains the scripts environment variables.

Talking to Maple and Mathematica

The following example illustrates how to send commands, and retrieve calculations and graphics.

#!/usr/local/bin/perl

$ENV{PATH} .=":/usr/local/bin:/u/share/bin";

#get the current process number

$pid = $$;

#open a connection to Maple

$maple = "| maple -q  > maple.$pid";
$maplegif = "maplegif.$pid";
open(MAPLE, $maple) || print STDERR "Maple can't be opened!\n";

#Send commands via print

print MAPLE "print(2 + 3); \n";
print MAPLE
"interface(plotdevice=gif,plotoutput=`$maplegif`,plotoptions=`width=500,height=300`);\n";
print MAPLE
"plot(x^2, x=-2..2);";
print MAPLE "quit;\n";
close(MAPLE);

#Set up some unique file names

$math_in = "math_in.$pid";
$math_out = "math_out.$pid";
$math_ps = "math_ps.$pid";
$math_gif = "math_gif.$pid";

#Construct a batch input file

open(MATH, "> $math_in") || print STDERR "Math can't be opened!\n";
print MATH "4 + 5\n";
print MATH "Display[\"$math_ps\",Plot[x^2, {x,-2,2},DisplayFunction->Identity]];\n";
print MATH "Quit \n";
close(MATH);

#process the file, convert the PS output

system("math -batchinput -batchoutput <$MATH_IN > $math_out");
system("psfix $math_ps | convert - $math_gif");
Another strategy for interacting with external programs is sending messages via a client-side helper applet. One implementation of this idea is the Hamlet project at the University of Utah. Further examples of web pages using this strategy can be found at the UCES web site at Ames National Laboratory.


Back: Strategies for Using Scripts
Up: Forms & Scripts


Presenting Mathematical Concepts on the World Wide Web. Copyright © 1997 by Carol Scheftic. All rights reserved. (This section was originally copyrighted in 1996 by The Geometry Center and is re-used here with permission.) Please send comments on this page, or requests for permission to re-use material from this page, to: scheftic@geom.umn.edu
Page established 1-Jun-97; last updated Thursday, 24-Jul-1997 02:05:31 CDT.