Father Chrysostomos asked:
> What I am really trying to find out is when the subroutine is actually
> cloned,
Yes. It is supposed to be (or at least must *appear* to be),
and currently is (or appears to be) in Rakudo.
> and whether there can be multiple clones within a single call of
> the enclosing sub.
Yes. For example, a lexical sub might be declared in a loop inside the
enclosing sub, in which case it should produce multiple instances, one
per iteration.
For example, this:
sub outer_sub () {
for (1..3) {
state $call_num = 1;
my sub inner_sub {
state $inner_state = (1..100).pick; # i.e. random number
say " [call {$call_num++}] \$inner_state = $inner_state";
}
say "\nsub id: ", &inner_sub.id;
inner_sub();
inner_sub();
}
}
outer_sub();
produces:
sub id: -4628941774842748435
[call 1] $inner_state = 89
[call 2] $inner_state = 89
sub id: -4628941774848253711
[call 3] $inner_state = 16
[call 4] $inner_state = 16
sub id: -4628941774839825925
[call 5] $inner_state = 26
[call 6] $inner_state = 26
under Rakudo
BTW, Both the above "yes" answers are consistent with (and can be
inferred from) the previous explanation that:
my sub foo { whatever() }
is just a syntactic convenience for:
my &foo := sub { whatever() }
HTH,
Damian