In this chapter we give an example of a CGI program written in Scheme using the LAML cgi library.
The name of the file must be number-guess.cgi , and it should be file protected to be executable (typically with chmod 755 ). This starts MzScheme via a shell script, and the load line is executed. It would be perfectly possible to place the entire Scheme program here, but we prefer in general to organize our software in a directory outside the .public_html catalogue. Therefore we load the number guessing program from the tutorial example directory in my LAML development directory, which happens to be /normark/scheme . As another important point, we decide to organize the number guessing system as a single program, which in the page-write part branches between a game init part ( ) and a game playing part ( ). We could as well have written two separate CGI programs, which probably would have been preferable in case the 'System' is larger. Such an alternative organization would typically call for common definitions organized in a shared scheme number guessing libary file.#!/bin/sh string=? ; exec /pack/mzscheme/bin/mzscheme \-r \$0 "\$\@" ;; This program implements a number guessing game, ;; which is commonly used to illustrate simple WWW ;; server programming. (define example-tutorial-dir "/user/normark/scheme/examples/tutorial/") (load (string-append example-tutorial-dir "cgi-programming/number-guess.scm"))
This ends the game preamble part.((language . "english") (mode . "init"))
((secret-number . "42") (players-guess . "22"))We prepare a body with a new form element (as above) ( ). The conditional ( ) compares the secret number with the players guess, and an appropriate hint is presented. Notice how guess-part is activated if no exact hit is found ( ). In the lucky case where the player guesses the number an URL is given to restart the game ( ).