In Variables§
See primary documentation in context for &?ROUTINE.
The compile time variable &?ROUTINE
provides introspection about which routine the program is actually within. It returns an instance of Routine
attached to the current routine. It does support the method .name
to obtain the name of the called routine, as well as .signature
and others method related to Routine
:
sub awesome-sub { say &?ROUTINE.name } awesome-sub; # OUTPUT: «awesome-sub»
It also allows also for recursion:
my $counter = 10; sub do-work { say 'Calling myself other ' ~ $counter-- ~ ' times'; &?ROUTINE() if ( $counter > 0 ); } do-work;
Note that, in a multi
, &?ROUTINE
refers to the current candidate, not the multi
as a whole.
Thus, the following recursive definition does not work:
multi broken-fibonacci($n where * ≤ 1) { $n } multi broken-fibonacci($n where * > 0) { &?ROUTINE($n - 1) + &?ROUTINE($n - 2) }
If called, &?ROUTINE
would always refer to the second multi candidate and would never dispatch to the first. If you want to use self-recursion for the whole proto
, either use the function name or samewith
.