如何从调用框架访问 $_ 变量?

Ric*_*rth 6 raku

在 Raku 中,我想替换$_<something>\xc6\xa4<something>where $_does Associative

\n

假设我有类似的东西

\n
my %h = %( aaa => { say $_<content> } );\n%h<aaa>.( %( :content('Hello world'), ) );\n
Run Code Online (Sandbox Code Playgroud)\n

这会产生Hello world.

\n

由于$_会出现很多次,我想将其替换为\xc6\xa4(不仅仅是为了保存一个字符,而是为了检查存在性等)。

\n

所以我想要的是:

\n
my %h = %( aaa => { say \xc6\xa4<content> } );\n%h<aaa>.( %( :content('Hello world  '), ) );\n
Run Code Online (Sandbox Code Playgroud)\n

我可以编写一个前缀运算符但是,我需要访问调用上下文的主题变量,即“aaa”指向的块内部sub prefix:<\xc6\xa4>(Str $key)的值。$_

\n

这是我尝试过的代码:

\n
sub prefix:<\xc6\xa4>(Str $key) {\n    my $topic = callframe(1).my<$_>;\n      # debug point\n    if $topic ~~ Associative and $topic{ $key }:exists {\n      # All good so return a string\n      return $topic{ $key }.Str\n    }\n      # Treat undefined as empty string\n    return ''\n}\n
Run Code Online (Sandbox Code Playgroud)\n

我遇到的问题是,在调试点,$topicRakudo::Internals::LoweredAwayLexical.

\n

我正在尝试的可能吗?

\n

Eli*_*sen 5

简而言之:使用CALLERS::

$_ = 42;
{ say CALLERS::<$_> }()  # 42
Run Code Online (Sandbox Code Playgroud)

有关更多信息,请参阅https://docs.raku.org/language/packages#Pseudo-packages