# # 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 # # # SetHandler perl-script # PerlHandler Hanoi # # # This file (renamed to Hanoi.pm) must be located in the path defined above. # # Usage # ----- # # http://server.name/hanoi/solve/ # # where 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(< The Towers Of Hanoi: *** error

The number of disks to solve for must be a positive integer between 1 and $MAXDISKS, both inclusive.

The Towers Of Hanoi URI is:

/hanoi/solve/<n>

where <n> is the number of disks.

ERROR return 400; } $r->print(< The Towers Of Hanoi: Solution List of moves for $disks disks:

SOLUTION_PRE do_hanoi($r, $disks, "1", "3", "2"); $r->print(< 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
\n"); } else { do_hanoi($r, $n - 1, $f, $u, $t); $r->print("move $f --> $t
\n"); do_hanoi($r, $n - 1, $u, $t, $f); } } 1;