On 05/26/2018 11:32 AM, Larry Wall wrote:
On Fri, May 25, 2018 at 07:23:45PM -0700, ToddAndMargo wrote:
: Follow up: based on Yary's wonderful advice, this is my keeper
: on the subject:
:
: <perl6.subs.name.of.sub.txt>
:
: perl6: what is the name of the subroutine you are currently in:
:
: It is:
: &?ROUTINE.name
: callframe(0).code.name
:
: $ p6 'sub flowers() { say "My subroutine name is <", &?ROUTINE.name,
: ">" }; flowers;'
: My subroutine name is <flowers>
:
:
: $ p6 'sub flowers() { say "My subroutine name is <",
: callframe(0).code.name, ">" }; flowers;'
: My subroutine name is <flowers>
Important caveat: the callframe(0) will only work at the top level of
the subroutine. It doesn't work in an inner block, while &?ROUTINE.name
does, since it locates the surrounding routine, however far out it needs
to scan. So, for example, if we add an extra set of braces:
$ p6 'sub flowers() { { say "My subroutine name is <", callframe(0).code.name,
">" } }; flowers;'
My subroutine name is <>
$ p6 'sub flowers() { { say "My subroutine name is <", &?ROUTINE.name, ">"
} }; flowers;'
My subroutine name is <flowers>
Larry
Thank you!
Love the examples too.