在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).效果很好.
I've used the CPAN Perl module Data::Printer (DP) with Perl. It works great.
现在我想在Raku代码中使用它.
Now I want to use it in Raku code.
当我使用:from<Perl5>
功能导入它,然后使用它运行代码时,注释(tied to Perl6::Hash)
会附加到散列的显示中. 1
When I use the :from<Perl5>
feature to import it and then run code using it, the annotation (tied to Perl6::Hash)
is appended to the display of hashes.1
如 DP的CPAN文档所示,此注释由选项show_tied
控制.我想将其设置为 off (将其设置为0
),而不是将其默认的 on (设置为1
).这是我在Perl中的做法:
As DP's CPAN doc shows, this annotation is controlled by the option show_tied
. I want to switch it off (set it to 0
) instead of its default on (set to 1
). Here's how I'd do that in Perl:
use Data::Printer show_tied => 0;
但是当我在Raku中尝试时:
But when I try this in Raku:
use Data::Printer:from<Perl5> show_tied => 0;
我得到:
从'Data :: Printer'导入时出错:没有这样的标签'show_tied'
Error while importing from 'Data::Printer': no such tag 'show_tied'
在Raku中使用DP时如何关闭show_tied
?
How do I turn show_tied
off when using DP in Raku?
1 也许是因为 Inline :: Perl5 (其中实现:from<Perl5>
功能)正在做一些事情来启用平滑的Perl/Raku互操作.
1 Perhaps this is because Inline::Perl5 (which implements the :from<Perl5>
feature) is doing something to enable smooth Perl/Raku interop.
推荐答案
在Raku中使用DP时如何关闭
show_tied
?
您必须明确转换 Associative
(例如列在use
语句末尾的 not "tags",到一个扁平的列表,交错键和值. 1
You must explicitly convert Associative
s (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
最直接的解决方案是手动编写平面文字列表,例如:
The most direct solution is to manually write a flat list of literals, eg:
use Data::Printer:from<Perl5> 'show_tied', 0;
有关更整洁的解决方案,请参阅下面的使用kv
部分.
For a neater solution, see the Using kv
section below.
请注意,use
语句是在编译时求值的.因此,如果要在列表中注入变量,则需要确保在评估use
语句之前,在 compile-time 处建立了它们的值,而不仅仅是它们的名称.没有修饰的my $foo = 0;
是不够的,因为= 0
部分将在运行时发生.相反,您将需要使用合适的编译时结构,例如 BEGIN
:>
Note that use
statements are evaluated at compile-time. So if you want to inject variables in the list then you need to ensure that their values, not just their names, are also established at compile-time, before the use
statement is evaluated. An unadorned my $foo = 0;
will not suffice because the = 0
part will happen at run-time. Instead you will need to use a suitable compile-time construct such as BEGIN
:
BEGIN my $foo = 0;
use Data::Printer:from<Perl5> 'show_tied', $foo;
使用kv
kv
例程可以生成所需的'key1', value1, 'key2', value2, ...
给定哈希值的序列:
Using kv
The kv
routine can generate the desired 'key1', value1, 'key2', value2, ...
sequence given a hash:
use Data::Printer:from<Perl5> kv { show_tied => 0 }
或:
BEGIN my %opts = show_tied => 0;
use Data::Printer:from<Perl5> kv %opts;
脚语
1 此答案基于Stefan从该问题的解释我响应在Raku中Data :: Printer中的更改参数" SO :
Footnotes
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;
胖逗号=>
是Raku中的Pair
构造函数,而实际上在Perl 5中只是花哨的逗号.Raku认为Pair
参数用于导入标签.类似于:ALL
(等效于ALL => True
).要解决此问题并传递Perl 5代码的期望,只需单独列出这些值即可.
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共享标签的概念(; 有关标签"的Raku文档),并且(并非偶然)习惯性地使用相同的语法来选择标签(:tagname
).
In other words, this need for conversion is because Perl and Raku share the notion of tags (Perl doc about "tags"; Raku doc about "tags") and (not coincidentally) idiomatically use the same syntax for selecting tags (:tagname
).
此外,使用Raku,这个问题(需要解决)之间的歧义(是否需要使用语法来指定标签)适用于在顶层广告代码中使用的 all Associative
use
语句,不仅是用:foo
形式编写的语句,甚至还包括用foo => bar
,{ foo => bar}
,%baz
或{ %baz }
等其他形式编写的语句.
Furthermore, using Raku, this issue of (the need to resolve) ambiguity between whether syntax is being used to specify tags or not applies to all Associative
s used in the top level of a use
statement, not just ones written in the form :foo
but even ones written in other forms such as foo => bar
, { foo => bar}
, %baz
, or { %baz }
.
这篇关于在Raku中使用Perl 5模块的Data :: Printer的show_tied选项时,该如何关闭?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!