在 Raku 中使用时,如何关闭 Perl 5 模块 Data::Printer 的 `show_tied` 选项?
How do I turn the Perl 5 module Data::Printer's `show_tied` option off when using it in Raku?
我在 Perl 中使用了 CPAN Perl 模块 Data::Printer (DP)。效果很好。
现在我想在 Raku 代码中使用它。
当我使用 :from<Perl5>
功能导入它然后 运行 使用它的代码时,注释 (tied to Perl6::Hash)
被附加到散列的显示中。1
如DP's CPAN doc 所示,此注释由选项show_tied
控制。我想将其切换为 off(将其设置为 0
)而不是默认的 on(设置为 1
) .下面是我在 Perl 中的做法:
use Data::Printer show_tied => 0;
但是当我在 Raku 中尝试这个时:
use Data::Printer:from<Perl5> show_tied => 0;
我得到:
Error while importing from 'Data::Printer': no such tag 'show_tied'
在 Raku 中使用 DP 时如何关闭 show_tied
?
脚注
1 也许这是因为 Inline::Perl5(它实现了 :from<Perl5>
功能)正在做一些事情来实现平滑的 Perl/Raku 互操作。
How do I turn show_tied
off when using DP in Raku?
您必须明确转换在 use
语句末尾列出的 Associative
s(例如 Pair
s),即 not "tags",到交错键和值的扁平化列表。1
最直接的解决办法是手动写一个扁平的文字列表,例如:
use Data::Printer:from<Perl5> 'show_tied', 0;
要获得更简洁的解决方案,请参阅下面的使用kv
部分。
注入变量
请注意 use
语句在 编译时 求值。所以如果你想在列表中注入变量,那么你需要确保它们的值,而不仅仅是它们的名字,也在编译时,在use
语句之前建立被评估。一个朴素的 my $foo = 0;
是不够的,因为 = 0
部分将在 运行-time 发生。相反,您需要使用合适的编译时构造,例如 BEGIN
:
BEGIN my $foo = 0;
use Data::Printer:from<Perl5> 'show_tied', $foo;
使用kv
kv
routine 可以生成所需的 'key1', value1, 'key2', value2, ...
给定哈希序列:
use Data::Printer:from<Perl5> kv { show_tied => 0 }
或:
BEGIN my %opts = show_tied => 0;
use Data::Printer:from<Perl5> kv %opts;
脚注
1 这个答案建立在 Stefan 来自 the issue I opened in response to the "Altering parameters in Data::Printer in Raku" SO 的解释之上:
The solution is rather simple: use Data::Printer:from<Perl5> 'show_tied', 0;
The fat comma =>
is a Pair
constructor in Raku while it's really just a fancy comma in Perl 5. Raku considers Pair
arguments to be used for importing tags like :ALL
(which is equivalent to ALL => True
). To get around this and pass what Perl 5 code expects, just list the values individually.
换句话说,这种转换的需要是因为 Perl 和 Raku 共享 概念 of tags (Perl doc about "tags"; Raku doc about "tags")并且(并非巧合)惯用地使用相同的语法来选择标签 (:tagname
)。
此外,使用 Raku,这个(需要解决的)语法是否被用于指定标签之间的歧义问题适用于 all Associative
s用于 use
语句的顶层,不仅是 :foo
形式的语句,甚至是 foo => bar
、{ foo => bar}
、[=39] 等其他形式的语句=], 或 { %baz }
.
我在 Perl 中使用了 CPAN Perl 模块 Data::Printer (DP)。效果很好。
现在我想在 Raku 代码中使用它。
当我使用 :from<Perl5>
功能导入它然后 运行 使用它的代码时,注释 (tied to Perl6::Hash)
被附加到散列的显示中。1
如DP's CPAN doc 所示,此注释由选项show_tied
控制。我想将其切换为 off(将其设置为 0
)而不是默认的 on(设置为 1
) .下面是我在 Perl 中的做法:
use Data::Printer show_tied => 0;
但是当我在 Raku 中尝试这个时:
use Data::Printer:from<Perl5> show_tied => 0;
我得到:
Error while importing from 'Data::Printer': no such tag 'show_tied'
在 Raku 中使用 DP 时如何关闭 show_tied
?
脚注
1 也许这是因为 Inline::Perl5(它实现了 :from<Perl5>
功能)正在做一些事情来实现平滑的 Perl/Raku 互操作。
How do I turn
show_tied
off when using DP in Raku?
您必须明确转换在 use
语句末尾列出的 Associative
s(例如 Pair
s),即 not "tags",到交错键和值的扁平化列表。1
最直接的解决办法是手动写一个扁平的文字列表,例如:
use Data::Printer:from<Perl5> 'show_tied', 0;
要获得更简洁的解决方案,请参阅下面的使用kv
部分。
注入变量
请注意 use
语句在 编译时 求值。所以如果你想在列表中注入变量,那么你需要确保它们的值,而不仅仅是它们的名字,也在编译时,在use
语句之前建立被评估。一个朴素的 my $foo = 0;
是不够的,因为 = 0
部分将在 运行-time 发生。相反,您需要使用合适的编译时构造,例如 BEGIN
:
BEGIN my $foo = 0;
use Data::Printer:from<Perl5> 'show_tied', $foo;
使用kv
kv
routine 可以生成所需的 'key1', value1, 'key2', value2, ...
给定哈希序列:
use Data::Printer:from<Perl5> kv { show_tied => 0 }
或:
BEGIN my %opts = show_tied => 0;
use Data::Printer:from<Perl5> kv %opts;
脚注
1 这个答案建立在 Stefan 来自 the issue I opened in response to the "Altering parameters in Data::Printer in Raku" SO 的解释之上:
The solution is rather simple: use
Data::Printer:from<Perl5> 'show_tied', 0;
The fat comma=>
is aPair
constructor in Raku while it's really just a fancy comma in Perl 5. Raku considersPair
arguments to be used for importing tags like:ALL
(which is equivalent toALL => True
). To get around this and pass what Perl 5 code expects, just list the values individually.
换句话说,这种转换的需要是因为 Perl 和 Raku 共享 概念 of tags (Perl doc about "tags"; Raku doc about "tags")并且(并非巧合)惯用地使用相同的语法来选择标签 (:tagname
)。
此外,使用 Raku,这个(需要解决的)语法是否被用于指定标签之间的歧义问题适用于 all Associative
s用于 use
语句的顶层,不仅是 :foo
形式的语句,甚至是 foo => bar
、{ foo => bar}
、[=39] 等其他形式的语句=], 或 { %baz }
.