#!/usr/bin/env perl

# Copyright (c) 2015 Christian Jaeger, copying@christianjaeger.ch
# This is free software. See the file COPYING.md that came bundled
# with this file.

use strict;
use warnings;
use warnings FATAL => 'uninitialized';

use Scalar::Util "weaken";

sub foo {
    my $f = sub {
        my ($f, $n) = @_;
        sub {
            if ($n > 0) {
                $n + &{ &$f($f, $n - 1) }
            } else {
                0
            }
        }
    };
    &$f($f, @_);
}

my $res = &{ foo 2 };

print $res, "\n";

