如何在Perl6中查看哈希的内容(类似于Perl 5模块的Data :: Dump或Data :: Show)? [英] 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)?
问题描述
在Perl 5中,如果我想查看哈希的内容,可以使用 Data::Show
, Data::Dump
或 Data::Dumper
.
In Perl 5, if I want to see the contents of a hash, I can use Data::Show
, Data::Dump
, or Data::Dumper
.
例如:
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);
哪个输出:
======( %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",
},
}
Perl 6中有什么等效的东西吗?我以为我记得达米安·康威(Damian Conway)在YAPC 2010上讨论了此功能,但此后我丢失了笔记,谷歌搜索没有帮助.
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;
我发现最接近工作的是%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
我正在使用在推荐答案
Perl6文档似乎建议在数据结构上使用 Perl6 docs seem to recommend using
.perl
,但看起来漂亮的印刷需要额外的工作:.perl
on the data structure, but it looks like pretty-printing requires extra work:
给出:
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.
这篇关于如何在Perl6中查看哈希的内容(类似于Perl 5模块的Data :: Dump或Data :: Show)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!