The Perl and Raku Conference 2025: Greenville, South Carolina - June 27-29 Learn more

#!/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 FATAL => 'uninitialized';
sub foo {
local *f = sub {
my ($n) = @_;
#f($n); # works, endless recursion.
#my $f = \&f; # does not work on either v5.14.2 or bleadperl
my $f = *f{CODE}; # neither does this (both give undef)
sub {
if ($n > 0) {
$n + &{ &$f($n - 1) }
} else {
0
}
}
};
f(@_);
}
my $res = &{ foo 2 };
print $res, "\n";