Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

For those interested in how Perl does dynamic scoping here is dynamic2.clj in perl5:

    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
PS. I seem to be making a lot of HN comments on dynamic scoping lately :)

http://news.ycombinator.com/item?id=3446384 | http://news.ycombinator.com/item?id=3423631



Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: