我该如何转向show_tied
在 Raku 中使用 DP 时关闭?
You must explicitly convert Associatives (eg Pair
s) that are listed at the end of a use
statement, that are not "tags", to a flattened list interleaving keys and values.1
最直接的解决办法就是manually写一个简单的文字列表,例如:
use Data::Printer:from<Perl5> 'show_tied', 0;
对于更简洁的解决方案,请参阅Using kv
下面的部分。
注入变量
注意use
语句的评估时间为编译时。因此,如果您想在列表中注入变量,那么您需要确保它们的值,而不仅仅是它们的名称,也建立在编译时, 之前use
声明被评估。一个朴素的my $foo = 0;
还不够,因为= 0
部分将发生在run-time。相反,您需要使用合适的编译时构造,例如BEGIN:
BEGIN my $foo = 0;
use Data::Printer:from<Perl5> 'show_tied', $foo;
Using kv
The kv routine可以生成想要的'key1', value1, 'key2', value2, ...
给定哈希值的序列:
use Data::Printer:from<Perl5> kv { show_tied => 0 }
or:
BEGIN my %opts = show_tied => 0;
use Data::Printer:from<Perl5> kv %opts;
脚注
1 This answer built upon Stefan's explanation from the issue I opened in response to the "Altering parameters in Data::Printer in Raku" SO:
解决方案相当简单:使用Data::Printer:from<Perl5> 'show_tied', 0;
胖逗号=>
is a Pair
Raku 中的构造函数,而在 Perl 5 中它实际上只是一个花哨的逗号。Raku 认为Pair
用于导入标签的参数,例如:ALL
(这相当于ALL => True
)。要解决这个问题并传递 Perl 5 代码所期望的内容,只需单独列出这些值即可。
换句话说,这种转换的需要是因为 Perl 和 Raku 共享notion of tags (关于“标签”的 Perl 文档; 关于“标签”的 Raku 文档) 和(并非巧合)惯用地使用相同的语法来选择标签 (:tagname
).
此外,使用 Raku,语法是否用于指定标签之间的歧义问题(需要解决)适用于all Associative
s 用于 a 的顶层use
声明,而不仅仅是表格中写的内容:foo
但即使是以其他形式编写的,例如foo => bar
, { foo => bar}
, %baz
, or { %baz }
.