Hi,

Why does anyone want to know the name of the sub inside the sub itself?

Is it more interesting to know the name of the calling sub?

Thanks!

Xin

> On May 26, 2018, at 2:32 PM, Larry Wall <la...@wall.org> 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

Reply via email to