Search code examples
functionintrospectionraku

Perl 6: Getting the name of function from inside it


In Perl 6, how can I get name of the function/subroutine from within its body, at runtime?

For example,

sub foo {
    say "My name is: " ~  <WHAT-API-HERE??> ;
}
...
foo();

The above code should print:

My name is: foo

I've looked in places like MOP, FAQ, and Functions.


Solution

  • sub foo { say &?ROUTINE.name }
    foo
    

    displays:

    foo
    

    See the &$ROUTINE doc.