简体   繁体   English

从Perl数据结构序列化YAML文档

[英]Serialize YAML document from Perl data structure

I'm attempting to print out a YAML document from a data structure, in particular, an array of hashes, which is what I think YAML::dump returns. 我正在尝试从数据结构(尤其是哈希数组)中打印出YAML文档,这就是我认为YAML :: dump返回的结果。 Here's the Perl code I'm using to build a YAML document and serialize it. 这是我用来构建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;

Here's what I want the YAML document to look like, with some Perl string substitutions. 这是我希望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

The following is the output of the console. 以下是控制台的输出。

---
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): ~

The Perl data structure you are using doesn't correspond to the YAML data that you require. 您使用的Perl数据结构与所需的YAML数据不对应。 It is only by chance that it compiles at all, and you must have seen a warning 它只是偶然地编译,您肯定已经看到警告

Odd number of elements in anonymous hash 匿名哈希中元素的奇数个

when you execute your code. 当您执行代码时。 Please don't ignore warnings, especially when asking for help with your code 请不要忽略警告,尤其是在寻求代码帮助时

In YAML, the lines element is an array, whereas your Perl data has it as a hash. 在YAML中, lines元素是一个数组,而您的Perl数据将其作为哈希值。 You need to replace the braces { ... } with square brackets [...] 您需要将方括号{ ... }替换为方括号[...]

I also suggest that you avoid YAML::Dump , which is a miconceived hack of the YAML::Tiny module. 我还建议您避免使用YAML::Dump ,这是对YAML::Tiny模块的误解。 YAML::XS is the preferred Perl implementation of YAML, and is a binding for the excellent libyaml library YAML::XSYAML::XS的首选Perl实现,并且是出色的libyaml库的绑定

This isn't a YAML problem; 这不是YAML问题。 lines should map to an arrayref, not a hashref: 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,
     }
     }
  }
};

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM