kernelthread.com

Perl

The Towers of Hanoi as a Perl script.

#! /usr/bin/perl # # The Towers Of Hanoi # Perl # Copyright (C) 1998 Amit Singh. All Rights Reserved. # http://hanoi.kernelthread.com # # Last tested under Perl 5.004_72 # ($#ARGV == 0) or die "usage: $0 N\n"; my $N = int($ARGV[0]); ($N > 0) or die "$0: illegal value for number of disks\n"; hanoi($N, 3, 1, 2); sub hanoi { my ($n, $to, $from, $using) = @_; if ($n > 0) { hanoi($n - 1, $using, $from, $to); print "move $from --> $to\n"; hanoi($n - 1, $to, $using, $from); } } #__END__

Download

hanoi.perl