在 Raku 中,我想替换$_<something>
为\xc6\xa4<something>
where $_
does Associative
。
假设我有类似的东西
\nmy %h = %( aaa => { say $_<content> } );\n%h<aaa>.( %( :content('Hello world'), ) );\n
Run Code Online (Sandbox Code Playgroud)\n这会产生Hello world
.
由于$_
会出现很多次,我想将其替换为\xc6\xa4
(不仅仅是为了保存一个字符,而是为了检查存在性等)。
所以我想要的是:
\nmy %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)
的值。$_
这是我尝试过的代码:
\nsub 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我遇到的问题是,在调试点,$topic
是Rakudo::Internals::LoweredAwayLexical
.
我正在尝试的可能吗?
\n简而言之:使用CALLERS::
$_ = 42;
{ say CALLERS::<$_> }() # 42
Run Code Online (Sandbox Code Playgroud)
有关更多信息,请参阅https://docs.raku.org/language/packages#Pseudo-packages
归档时间: |
|
查看次数: |
140 次 |
最近记录: |