Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Altering parameters in Data::Printer in Perl6

Tags:

printing

raku

I'm printing data in Perl6 with Data::Printer which is a spectacular package, but I am trying to alter parameters and I am not able to.

For example, I want:

HG00112 {
    gained_site                    {
        9:10162   0,
        9:10272   var{HG00112}{gained_site}{9:10162},
        9:10326   var{HG00112}{gained_site}{9:10162},
        ...
}(tied to Perl6::Hash)

to look like

HG00112 {
    gained_site                    {
        9:10162   0,
        9:10272   0,
        9:10326   0,
        ...
}(tied to Perl6::Hash)

for easier readability (I don't care about tied to Perl6::Hash specifically)

this hash element can be seen with JSON:

"HG00112": {
    "discordant_multiallelic_loss": 0,
    "concordant_hom_alt": 4,
    "discordant_het_to_alt": 0,
    "discordant_hom_alt_to_ref": 0,
    "discordant_hom_ref_to_alt": 0,
    "lost_site": 0,
    "concordant_het": 3,
    "discordant_multiallelic_gain": 0,
    "discordant_hom_alt_to_het": 0,
    "discordant_call_to_no_call": 0,
    "discordant_het_to_ref": 0,
    "concordant_hom_ref": 5,
    "concordant_site": 18,
    "discordant_no_call_to_call": 0,
    "concordant_no_call": 6,
    "concordant_multiallelic": 0,
    "gained_site": 0,
    "discordant_hom_ref_to_het": 0
}

I normally load the package using use Data::Printer:from<Perl5>, and using suggestions from

Terminal ANSI colors does not work with Inline::Perl5 (Data::Printer)

I have tried using that with advice from https://metacpan.org/pod/Data::Printer , namely

use Data::Printer:from<Perl5> {show_tied => 0}

& use Data::Printer:from<Perl5> show_tied => 0

but both show the error

Error while importing from 'Data::Printer': no such tag 'show_tied'

How can I get the output from Data::Printer to look like the second code selection, without the ugly var{...?

---edit---

a slight improvement, the script recognizes the option show_tied but still doesn't use it:

my test script:

use JSON::Fast;
use Data::Printer:from<Perl5> 'show_tied', 0;

my %conc = from-json '{"HG00112": {
    "discordant_multiallelic_loss": 0,
    "concordant_hom_alt": 4,
    "discordant_het_to_alt": 0,
    "discordant_hom_alt_to_ref": 0,
    "discordant_hom_ref_to_alt": 0,
    "lost_site": 0,
    "concordant_het": 3,
    "discordant_multiallelic_gain": 0,
    "discordant_hom_alt_to_het": 0,
    "discordant_call_to_no_call": 0,
    "discordant_het_to_ref": 0,
    "concordant_hom_ref": 5,
    "concordant_site": 18,
    "discordant_no_call_to_call": 0,
    "concordant_no_call": 6,
    "concordant_multiallelic": 0,
    "gained_site": 0,
    "discordant_hom_ref_to_het": 0
}}';

p %conc;

shows nearly useless output:

{
    HG00112   {
        concordant_het                 3,
        concordant_hom_alt             var{HG00112}{concordant_het},
        concordant_hom_ref             var{HG00112}{concordant_het},
        concordant_multiallelic        var{HG00112}{concordant_het},
        concordant_no_call             var{HG00112}{concordant_het},
        concordant_site                var{HG00112}{concordant_het},
        discordant_call_to_no_call     var{HG00112}{concordant_het},
        discordant_het_to_alt          var{HG00112}{concordant_het},
        discordant_het_to_ref          var{HG00112}{concordant_het},
        discordant_hom_alt_to_het      var{HG00112}{concordant_het},
        discordant_hom_alt_to_ref      var{HG00112}{concordant_het},
        discordant_hom_ref_to_alt      var{HG00112}{concordant_het},
        discordant_hom_ref_to_het      var{HG00112}{concordant_het},
        discordant_multiallelic_gain   var{HG00112}{concordant_het},
        discordant_multiallelic_loss   var{HG00112}{concordant_het},
        discordant_no_call_to_call     var{HG00112}{concordant_het},
        gained_site                    var{HG00112}{concordant_het},
        lost_site                      var{HG00112}{concordant_het}
    } (tied to Perl6::Hash)
} (tied to Perl6::Hash)
like image 553
con Avatar asked Apr 22 '19 18:04

con


People also ask

What is the use of print operator in Perl?

print () operator – print operator in Perl is used to print the values of the expressions in a List passed to it as an argument. Print operator prints whatever is passed to it as an argument whether it be a string, a number, a variable or anything. Double-quotes (“”) is used as a delimiter to this operator.

How do you print content in Perl?

The semicolon (;) is needed for Perl print content. print (“ display content write here… ”); The double quotes “…” are placed inside of the simple bracket (“…”) and write content and parameters. How does the print Function Work in Perl?

What is the difference between say() and print() function in Perl?

These characters will be printed as it is and will not be evaluated. say () function in Perl works similar to the print () function but there’s a slight difference, say () function automatically adds a new line at the end of the statement, there is no need to add a newline character ‘ ’ for changing the line.

Which version of Perl is used to execute the say() function?

Here, we have used ‘ use 5.010 ‘ to use the say () function because newer versions of Perl don’t support some functions of the older versions and hence, the older version is to be called to execute the say () function.


1 Answers

As explained in the Q+A I introduce and link below, you should be able to control DP's name option as follows:

use Data::Printer kv { name => '' }

This will hopefully eliminate at least the var from "the ugly var{..." bit.

And if so, that in turn will at least demonstrate that you are now able to control DP's options.

At best, setting name to '' will switch off the whole circular reference display. (And if not, maybe setting it to 0?)

At worst, you'll still see the three letter "name" var at the start of the circular references and I think we'd then have to figure out why.

If you can comment on how things worked out with kv { name => '' } we'll be making progress.

How do I turn the Perl 5 module Data::Printer's show_tied option off when using it in Raku?

A key part of your question that you explicitly "don't care about ... specifically" (the (tied to Perl6::Hash) bit, which is controlled by DP's show_tied option) is nevertheless very much worth clearly resolving. If nothing else, it involves what will inevitably be a FAQ when trying to set, in a Raku use statement, almost any named option of almost any Perl module.

So I've written a Q+A pair resolving that bit: How do I turn the Perl 5 module Data::Printer's show_tied option off when using it in Raku?.

"the ugly var{..."

The first and most important point I can make right now is that, just like @HåkonHægland++ in one of their comments on your question, I cannot reproduce your problem.

That is to say, when I run your last listed test code, it displays (with some lines elided at ...):

{
    HG00112   {
        concordant_het                 3,
        concordant_hom_alt             4,
        concordant_hom_ref             5,
        concordant_multiallelic        0,
        concordant_no_call             6,
        concordant_site                18,
        discordant_call_to_no_call     0,
        ...
        discordant_no_call_to_call     0,
        gained_site                    0,
        lost_site                      0
    }
}

As you can see, there's none of the "ugly" circular references.

This is with show_tied set to off, as per the above linked Q+A. But even with show_tied left on, per the default, I still get the above, just with a couple (tied to Perl6::Hash) annotations.

So, as @HåkonHægland++ explained in another of their comments on your question, the var{... parts are presumably nothing to do with show_tied but rather due to circular references in your JSON data that you are not sharing with us.

In short, your problem is something about your data and/or about use of DP's name option, not its show_tied option.

I think it's unlikely any of us Raku folk can help with your data (which was presumably not generated by Raku) but we should be able to help make progress and perhaps even resolve your question to the degree it's about appropriately using some DP option.

like image 86
raiph Avatar answered Oct 05 '22 09:10

raiph