我试图从数据结构中打印出一个YAML文档,特别是一个哈希数组,这就是我认为YAML :: dump返回的内容。这是我用来构建YAML文档并将其序列化的Perl代码。
my @tagobj_header_table =
{
core => {
type => $tagobj_type,
size => $tagobj_size,
blob => $tagobj_blob,
},
text => {
lines => {
{
offset => 0,
length => 1
},
{
offset => 1,
length => 5,
},
{
offset => 6,
length => 7,
},
{
offset => 13,
length => 13,
},
{
offset => 26,
length => 1,
}
}
}
};
my $dumper = YAML::Dumper->new;
my $tagobj_contents = $dumper->dump(@tagobj_header_table);
print $tagobj_contents;
这是我希望YAML文档看起来像什么,有一些Perl字符串替换。
---
core:
type: $tagobj_type
size: $tagobj_size
blob: $tagobj_blob
text:
lines:
- offset: 0
length: 1
- offset: 1
length: 5
- offset: 6
length: 7
- offset: 13
length: 13
- offset: 26
length: 1
以下是控制台的输出。
---
core:
blob: build\content\objects\36d80951814b5f08c7ba34cd7a5459b4c212ee6200ce247ac2a13d24b2fc0d57
size: 31
type: blob/text
text:
lines:
HASH(0x4d1b840):
length: 13
offset: 13
HASH(0x4d1df38):
length: 5
offset: 1
HASH(0x4d1eee8): ~
答案 0 :(得分:2)
您使用的Perl数据结构与您需要的YAML数据不对应。它只是偶然编译,你必须看到警告
匿名哈希中奇数个元素
执行代码时。请不要忽略警告,特别是在寻求代码帮助时
在YAML中,lines
元素是一个数组,而您的Perl数据将其作为哈希。您需要用方括号{ ... }
[...]
我还建议您避免YAML::Dump
,这是YAML::Tiny
模块的微观黑客攻击。 YAML::XS
是YAML的首选Perl实现,是优秀libyaml
库的绑定
答案 1 :(得分:0)
这不是YAML问题; lines
应映射到arrayref,而不是hashref:
text => {
lines => { # this is an hashref, so
{
offset => 0,
length => 1
}, # this is a hash key, and gets stringified: "HASH(0x4d1df38)"
{
offset => 1,
length => 5,
}, # and this is a hash value
{
offset => 6,
length => 7,
}, # this is a key, also stringified: "HASH(0x4d1b840)"
{
offset => 13,
length => 13,
}, # and this is a value...
{
offset => 26,
length => 1,
}
}
}
};