Skip to content

Commit d79a561

Browse files
committed
Improve the &?ROUTINE section also with recursion example.
1 parent 2717102 commit d79a561

File tree

1 file changed

+19
-1
lines changed

1 file changed

+19
-1
lines changed

doc/Language/variables.pod6

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1078,11 +1078,29 @@ With particular regard to the C<$?NL>, see the L<newline pragma|/language/pragm
10781078
10791079
=head3 X<&?ROUTINE|&?ROUTINE>
10801080
1081-
Which routine am I in? Has method C<.name>
1081+
The compile time variable C<&?ROUTINE> provides introspection about
1082+
which routine the program is actually within. It returns an instance
1083+
of L<Sub|/type/Sub> attached to the current routine.
1084+
It does support the method C<.name> to obtain
1085+
the name of the called routine, as well as C<.signature> and others
1086+
method related to C<Sub>:
1087+
10821088
10831089
sub awesome-sub { say &?ROUTINE.name }
10841090
awesome-sub # OUTPUT: awesome-sub
10851091
1092+
The special variable C<&?ROUTINE> allows also for recursion:
1093+
1094+
=begin code
1095+
my $counter = 10;
1096+
sub do-work {
1097+
say 'Calling myself other ' ~ $counter-- ~ ' times';
1098+
&?ROUTINE() if ( $counter > 0 );
1099+
}
1100+
do-work;
1101+
=end code
1102+
1103+
10861104
=begin code
10871105
for '.' {
10881106
.Str.say when !.IO.d;

0 commit comments

Comments
 (0)