How do I turn show_tied
off when using DP in Raku?
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;
For a neater solution, see the Using kv
section below.
Injecting variables
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;
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 }
or:
BEGIN my %opts = show_tied => 0;
use Data::Printer:from<Perl5> kv %opts;
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:
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.
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
).
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 }
.