How can I view the contents of a hash in Perl6 (in a fashion similar to the Perl 5 modules Data::Dump or Data::Show)?
Asked Answered
C

3

10

In Perl 5, if I want to see the contents of a hash, I can use Data::Show, Data::Dump, or Data::Dumper.

For example:

use Data::Show;

my %title_for = (
    'Book 1' => {
        'Chapter 1' => 'Introduction',
        'Chapter 2' => 'Conclusion',
    },
    'Book 2' => {
        'Chapter 1' => 'Intro',
        'Chapter 2' => 'Interesting stuff',
        'Chapter 3' => 'Final words',
   }
);

show(%title_for);

Which outputs:

======(  %title_for  )======================[ 'temp.pl', line 15 ]======

    {
      "Book 1" => { "Chapter 1" => "Introduction", "Chapter 2" => "Conclusion" },
      "Book 2" => {
                    "Chapter 1" => "Intro",
                    "Chapter 2" => "Interesting stuff",
                    "Chapter 3" => "Final words",
                  },
    }

Is there anything equivalent in Perl 6? I thought I remember Damian Conway discussing this feature at YAPC 2010, but I have since lost my notes and Googling hasn't helped.

use v6;

my %title_for = (
  "Book 1" => { "Chapter 1" => "Introduction", "Chapter 2" => "Conclusion" },
  "Book 2" => {
                "Chapter 1" => "Intro",
                "Chapter 2" => "Interesting stuff",
                "Chapter 3" => "Final words",
              },
);

%title_for.say;

The closest thing that I found to working is %title_for.say. However, it seems messy for nested hashes:

Book 1 => Chapter 1 => Introduction, Chapter 2 => Conclusion, Book 2 => Chapter 1 => Intro, Chapter 2 => Interesting stuff, Chapter 3 => Final words

I'm using Perl6 running on MoarVM from the January 2015 release of Rakudo Star.

Courageous answered 17/2, 2015 at 15:48 Comment(0)
S
7

Perl6 docs seem to recommend using .perl on the data structure, but it looks like pretty-printing requires extra work:

Given:

my @array_of_hashes = (
    { NAME => 'apple',   type => 'fruit' },
    { NAME => 'cabbage', type => 'no, please no' },
);

Perl 5

use Data::Dumper;
$Data::Dumper::Useqq = 1;
print Dumper \@array_of_hashes; # Note the backslash.

Perl 6

say @array_of_hashes.perl; # .perl on the array, not on its reference.
Scarper answered 17/2, 2015 at 16:31 Comment(1)
That is exactly what I was trying to remember: .perl. Thanks!Courageous
F
3

I have since made a PrettyDump module that might be handy:

use v6;
use PrettyDump;

my %title_for = (
  "Book 1" => { "Chapter 1" => "Introduction", "Chapter 2" => "Conclusion" },
  "Book 2" => {
                "Chapter 1" => "Intro",
                "Chapter 2" => "Interesting stuff",
                "Chapter 3" => "Final words",
              },
);

say PrettyDump.new.dump: %title_for;

With output:

Hash={
    :Book 1(Hash={
        :Chapter 1("Introduction"),
        :Chapter 2("Conclusion")
    }),
    :Book 2(Hash={
        :Chapter 1("Intro"),
        :Chapter 2("Interesting stuff"),
        :Chapter 3("Final words")
    })
}

There are some formatting options you can give to PrettyDump too.

Flann answered 3/7, 2017 at 13:18 Comment(0)
C
2

dd for Data Dump in rakudo.

my @array_of_hashes = (
    { NAME => 'apple',   type => 'fruit' },
    { NAME => 'cabbage', type => 'no, please no' },
);
dd @array_of_hashes;
Cloudland answered 16/3, 2020 at 14:40 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.