📘 Nested subs in Perl 6

Nested subs are allowed in Perl 6.

sub cube($x) {
    sub square($x) {
        return $x * $x;
    }

    return $x * square($x);
}

say cube(3); # 27

The name of the inner sub square is only visible within the body of the outer sub cube.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s