use strict; use warnings; #=============================================== # THE GAME PROTOTYPE #=============================================== my %VARIABLES; # child picks a kind of problem: Z = (X+Y)^2 # we parse their formula and set up something like this: my $aVariables = [ 'X', 'Y' ]; my $sEquation = "(X + Y)^2"; my $X = getVar('X'); my $Y = getVar('Y'); my $Z = multiply(add($X,$Y),add($X,$Y)); # now we play a game where we calculate the solution using # different variable inputs for (1..5) { # assign values to variables and print out the problem # the print out looks something like this: # "Problem #1: if X=1 and Y=4 and ...., what is X + Y + ... ?" print "\nproblem $_: If " . join(' and ' , map { $VARIABLES{$_} = int rand(10) + 1; "$_=$VARIABLES{$_}" } @$aVariables) . ", what is $sEquation ?\n"; print "hey! you got it right: the answer is "; print "Z = " . &$Z . "\n\n"; } #=============================================== # THE FUNCTION GENERATORS #=============================================== sub getVar { my $x = shift; return sub { return $VARIABLES{$x}; } } sub makeFunc { my $x = shift; return ref($x) eq 'CODE' ? $x : sub { return $x; }; } sub add { my ($x, $y) = @_; my $crX = makeFunc($x); my $crY = makeFunc($y); return sub { my $result= &$crX + &$crY; #print STDOUT "\tevaluating: $x + $y => $result\n"; return $result; }; } sub multiply { my ($x, $y) = @_; my $crX = makeFunc($x); my $crY = makeFunc($y); return sub { my $result= &$crX * &$crY; #print STDOUT "\tevaluating: $x*$y => $result\n"; return $result; } };