Here you can find a Perl implementation of the (external link!) game of nim.

Please read my Disclaimer.

########################################################################
#
# File:        nim.pl
# Author:      Bernd Plumhoff
# Version:     1.1
#
# Usage:       Game of Nim
#
# Call:        perl nim.pl
#
########################################################################
#

sub zuende {
        $summe = $h[0];
        for ($i=1; $i<$haufen; $i++) {
                $summe += $h[$i];
        }
        $summe == 0;
}

sub zeigen {
        print "\n";
        for ($i=0; $i<$haufen; $i++) {
                print "\tHeap ", $i, ":\t", $h[$i], "\n";
        }
        print "\n";
}
      
sub computer {
        # Computer to move

        $computer=0;     
        for ($i=0; $i<$haufen; $i++) {
                $computer^=$h[$i];
        }

        if ($computer==0) {
                # Computer is going to lose
                for ($i=0; $h[$i]==0; $i++) {
                        ;
                }
                $computer = $h[$i];
        } else {
                # Computer will win
                for ($i=0; ($i<$haufen) && (($computer^$h[$i])>=$h[$i]); $i++) {
                        ;
                }
                $computer = $h[$i] - ($computer ^ $h[$i]);
        }

        print "\tI take ", $computer, " stone(s) from heap ", $i, ".\n";
       
        $h[$i] -= $computer;
       
}

sub mensch {
        # Human to move

        do {
                print "\tWhich heap do you want to take stones from? ";
        } while ((($hnr=<>)<0 || $hnr>=$haufen || $h[$hnr]==0) && print "\tWrong heap number!\n\n");
       
        do {
                print "\tHow many stones do you want to take from heap ", $hnr+0, "? ";
        } while ((($anz=<>)<=0 || $h[$hnr]<$anz) && print "\tWrong number of stones!\n\n");
       
        $h[$hnr] -= $anz;
}

# Main program

print "\n\t\t\tN I M\n\t\t\t=====\n\n\tThis is the game of Nim. You can take as  ";
print "many stones from\n\tone heap as you like (at least one)";
print "\n\tIf you take the last stone of the last heap you win!\n";
print "\n\n";

do {
        print "\tHow many heaps (at least 3)? ";
} while ((($haufen=<>) < 3) && print "\tYou need to choose at least 3 heaps!\n\n");

print "\n";

for ($i=0; $i<$haufen; $i++) {
        do {
                print "\tHow many stones for heap number ", $i, "? ";
        } while ((($h[$i]=<>+0) <= 0) && print "\tYou need to have at least one!\n\n");
}

while () {
        &zeigen();

        &computer();

        &zuende() && die "\tI won!\n";
       
        &zeigen();

        &mensch();

        &zuende() && die "\tYou won!\n";
}

<>;