Perl Source Code
# Author: Hannes du Plooy
# Date: 20 Sep 2016
# Objective: To solve the towers of hanoi with number of discs provided in the arguments to calling the program
sub hanoi {
my $source = $_[0];
my $dest = $_[1];
my $other = $_[2];
my $discs = $_[3];
if ($discs > 0) {
hanoi($source,$other,$dest,$discs-1);
print "Move ",$source," to ",$dest,"\n";
hanoi($other,$dest,$source,$discs-1);
}
}
$num_args = $#ARGV + 1;
if ($num_args != 1) {
print "Please provide number of discs\n";
exit;
}
hanoi(0,2,1,$ARGV[0]);