Oh this works too sub my-name-is-Sue () { say "Hi, ", &?ROUTINE.name } my-name-is-Sue;
-y On Tue, May 22, 2018 at 8:59 AM, yary <not....@gmail.com> wrote: > I was surprised there isn't a cleaner way to get a sub/method names, and > found https://docs.perl6.org/language/5to6-perlfunc#caller > > Try this tasty morsel > > who-am-i () { say "I'm ", callframe(0).code.name } > who-am-i; > > > -y > > On Tue, May 22, 2018 at 12:41 AM, ToddAndMargo <toddandma...@zoho.com> > wrote: > >> On 05/21/2018 11:21 PM, Siavash wrote: >> >>> >>> https://www.nntp.perl.org/group/perl.perl6.users/2017/03/msg3423.html >>> >>> On 2018-05-22 06:04:47 GMT, ToddAndMargo wrote: >>> >>>> Hi All, >>>> >>>> I need to know the name of the subroutine I am in. >>>> >>>> This is the way I use to do it in Perl 5: >>>> >>>> (my $Name = (caller(0))[3] ) ~~ s{.*::}{}; >>>> >>>> How do I do it in Perl 6? >>>> >>>> Many thanks, >>>> -T >>>> >>> >> Hi Siavash, >> >> Oh now this is embarrassing. That was me! And I >> have a keeper file on it too. >> >> Wait, why am I wring you again? Is Sparky still >> in the navy? Is Teddy still president? You kids >> get off my lawn !!! I hate when this happens. >> I have a lawn? >> >> :'( >> >> -T >> >> My newly rediscovered notes: >> >> >> Perl 6: >> what is my program's name? >> what is my sub's name? >> >> <code WhoTest.pl6> >> #!/usr/bin/perl6 >> >> sub Test () { >> >> my $f = $?FILE; say " \$\?FILE=<$f>"; >> my $g = $?FILE.IO.basename; say " \$\?FILE.IO.basename=<$g>"; >> ( my $IAm = $?FILE ) ~~ s|.*"/"||; say " Regex: \$IAm=<$IAm>\n"; >> >> >> # sub Test () { #`(Sub|58588296) ... } >> my $h = &?ROUTINE.gist; >> say " \&\?ROUTINE.gist=<$h>"; >> $h ~~ m/' '(.*?)' '\(/; >> say " Regex: This sub\'s name is <$0>"; >> >> &?ROUTINE.gist ~~ m/' '(.*?)' '\(/; say " A more compact way =<$0>;" >> } >> >> >> Test(); >> </code> >> abc >> >> >> <code WhoIsMySub.pl6> >> #!/usr/bin/env perl6 >> >> sub f() { put &?ROUTINE.gist; }; >> >> sub abc () { >> say "This subroutine's ID is ", f; >> print "\n"; >> >> &?ROUTINE.gist ~~ m/' '(.*?)' '\(/; >> my $SubName = $0; >> say "This subroutine is called $SubName"; >> } >> </code> >> >> >> >> $ WhoTest.pl6 >> $?FILE=</home/linuxutil/./WhoTest.pl6> >> $?FILE.IO.basename=<WhoTest.pl6> >> Regex: $IAm=<WhoTest.pl6> >> >> &?ROUTINE.gist=<sub Test () { #`(Sub|72467224) ... }> >> Regex: This sub's name is <Test> >> >> A more compact way =<Test>; >> >> Gist reference: >> https://docs.perl6.org/routine/gist#class_Mu >> > >