For those interested in how Perl does dynamic scoping here is dynamic2.clj in perl5:<p><pre><code> use 5.014;
use warnings;
our ($x, $y) = (2, 2);
sub sum_of_squares {
my ($a, $b) = @_;
($a * $a) + ($b * $b);
}
sub sum_of_squares_for_x_and_y { sum_of_squares $x, $y }
say sum_of_squares_for_x_and_y; # => 8
{
local ($x, $y) = (10, 5);
no warnings 'redefine';
local *sum_of_squares = sub {
my ($a, $b) = @_;
$a / $b;
};
say sum_of_squares_for_x_and_y; # => 2
}
say sum_of_squares_for_x_and_y; # => 8
</code></pre>
PS. I seem to be making a lot of HN comments on dynamic scoping lately :)<p><a href="http://news.ycombinator.com/item?id=3446384" rel="nofollow">http://news.ycombinator.com/item?id=3446384</a> | <a href="http://news.ycombinator.com/item?id=3423631" rel="nofollow">http://news.ycombinator.com/item?id=3423631</a>