Created
January 26, 2020 15:52
-
-
Save thundergnat/c74fc2753a8577830357593b237f152f to your computer and use it in GitHub Desktop.
wrapped routine caller name?
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| use soft; | |
| my @log; | |
| sub loggit (*@args) { | |
| my $return = try { callwith(|@args) }; | |
| #say &?ROUTINE.name; # Off by one call frame | |
| #say (&::CALLER::&?ROUTINE).name; # just plain wrong | |
| #.say for Backtrace.new.list; # the wrapped sub name doesn't appear in the backtrace | |
| =begin fail | |
| # another failed attempt at finding the calling routine name | |
| die; | |
| CATCH { | |
| when X::AdHoc { | |
| my @frames = .backtrace[*]; | |
| .say for @frames; | |
| .resume; | |
| } | |
| default {} | |
| } | |
| =end fail | |
| # I want to somehow determine "root", "plus-one" or "div-two" | |
| push @log: "(the actual calling routine name): {@args.join(' ')} -> $return"; | |
| $return | |
| } | |
| sub root ($a) { sqrt $a } | |
| sub plus-one ($a) { $a + 1 } | |
| sub div-two ($a) { $a / 2 } | |
| &root .wrap(&loggit); | |
| &plus-one.wrap(&loggit); | |
| &div-two .wrap(&loggit); | |
| say 5.&(&div-two o &plus-one o &root); | |
| .say for @log; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment