kernelthread.com

Apache Web Server Module in Perl

The Towers of Hanoi as an Apache Web Server Module in Perl. More information on the Apache Web Server can be found at httpd.apache.org.

In order to use this module you have to add the following lines to the web server configuration file (typically httpd.conf):

PerlSetEnv PERL5LIB /path/to/some/directory <Location /hanoi/solve> SetHandler perl-script PerlHandler Hanoi </Location>

The source file must be renamed to Hanoi.pm, and must be located in the path defined above in the configuration file.

Thereafter, you can use the URL http://server.name/hanoi/solve/n to generate the solution for a problem consisting of n disks.

# # The Towers Of Hanoi # Apache Web Server Module (Perl) # Copyright (C) 2001 Amit Singh. All Rights Reserved. # http://hanoi.kernelthread.com # # # Configuration # ------------- # # Add the following lines to httpd.conf: # # PerlSetEnv PERL5LIB /path/to/some/directory # # <Location /hanoi/solve> # SetHandler perl-script # PerlHandler Hanoi # </Location> # # This file (renamed to Hanoi.pm) must be located in the # path defined above. # # Usage # ----- # # http://server.name/hanoi/solve/<n> # # where <n> is the number of disks to solve for # package Hanoi; use strict; use Apache::Constants qw(:common); my $MAXDISKS = 10; sub handler { my $r = shift; $r->content_type('text/html'); $r->send_http_header; $_ = $r->uri; my $disks = 0; if (/\/hanoi\/solve\/(\d+)/) { $disks = int($1); } if (($disks < 0) || ($disks > $MAXDISKS)) { $r->print(<<ERROR); <html> <head> <title>The Towers Of Hanoi: *** error</title> </head> <body> <p> The number of disks to solve for must be a positive integer between 1 and $MAXDISKS, both inclusive. <p> The Towers Of Hanoi URI is: <p> /hanoi/solve/&lt;n&gt; <p> where &lt;n&gt; is the number of disks. <p> </body> </html> ERROR return 400; } $r->print(<<SOLUTION_PRE); <html> <head> <title>The Towers Of Hanoi: Solution</title> </head> <body> List of moves for $disks disks:<p> SOLUTION_PRE do_hanoi($r, $disks, "1", "3", "2"); $r->print(<<SOLUTION_POST); </body> </html> SOLUTION_POST } sub do_hanoi { my $r = shift; my $n = shift; my $f = shift; my $t = shift; my $u = shift; if ($n <= 1) { $r->print("move $f --> $t<br>\n"); } else { do_hanoi($r, $n - 1, $f, $u, $t); $r->print("move $f --> $t<br>\n"); do_hanoi($r, $n - 1, $u, $t, $f); } } 1;

Download

hanoi.apache-perl