sub make_estimator { return sub {0} unless @_; # You might want to die? my @points = @_; return sub { my $weight_total; my $weighted_sum; foreach my $point (@points) { my $dist_squared = 0; foreach my $coord (0..$#_) { $dist_squared += ($point->[$coord] - $_[$coord])**2; } if (0 == $dist_squared) { return $point->[-1]; } else { my $weight = 1/$dist_squared; $weight_total += $weight; $weighted_sum += $weight * $point->[-1]; } } return $weighted_sum/$weight_total; }; } my $estimator = make_estimator( [3, 4], [4, 5], ); print $estimator->(3.5);