Perl:将多个标题写入单个YAML文档

时间:2018-05-06 22:40:11

标签: perl yaml

以下是我想要的输出YAML文档。

---
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

这是我用来创建要序列化的YAML文档的代码。

sub make_tagobj_core_header {
 return ({
    core =>  {
     type => $tagobj_type,
     size => $tagobj_size,
     blob => $tagobj_blob,
     }
  });
}

sub make_tagobj_text_header {
    my @tagobj_text_lines;
    my ($lineno, $length, $offset);

    $offset = 0;
    while(my $line = <$object_fh>) {
        $lineno = $. - 1;
        $length = length($line);

        push @tagobj_text_lines, {
            offset => $offset,
            length => $length,
        };
        $offset += $length;
    }

    return ({
        text => {
                lines => [@tagobj_text_lines]
            }
        })
    } 
}

my @tagobj_header_table;
push @tagobj_header_table, make_tagobj_core_header;
push @tagobj_header_table, make_tagobj_text_header;

my $dumper = YAML::Dumper->new;
print $dumper->dump(@tagobj_header_table);

这是输出。

---
core:
  blob: build\content\objects\36d80951814b5f08c7ba34cd7a5459b4c212ee6200ce247ac2a13d24b2fc0d57
  size: 31
  type: blob/text
---
text:
  lines:
    - length: 1
      offset: 0
    - length: 5
      offset: 1
    - length: 7
      offset: 6
    - length: 13
      offset: 13
    - length: 1
      offset: 26

我哪里出错了?我想我需要一系列哈希作为我在YAML中的顶级实体,但是它们被打印成单独的文件,由&#39; ---&#39;分隔。 我想在不破解YAML :: Dumper :: dump的输出的情况下删除它,因为我在单个文件中有多个结构相同的YAML文档的用例,这是规范支持的。

1 个答案:

答案 0 :(得分:5)

你似乎已经知道了,

a: b
c: d

是通过转储哈希获得的,因此您希望将哈希作为文档的顶级。概念证明:

print YAML::Dumper->new->dump({
   core => {
      blob => 'build\\content\\objects\\36d80951814b5f08c7ba34cd7a5459b4c212ee6200ce247ac2a13d24b2fc0d57',
      size => 31,
      type => 'blob/text',
   },
   text => {
      lines => [
         { length =>  1, offset =>  0 },
         { length =>  5, offset =>  1 },
         { length =>  7, offset =>  6 },
         { length => 13, offset => 13 },
         { length =>  1, offset => 26 },
      ],
   },
});

输出:

---
core:
  blob: build\content\objects\36d80951814b5f08c7ba34cd7a5459b4c212ee6200ce247ac2a13d24b2fc0d57
  size: 31
  type: blob/text
text:
  lines:
    - length: 1
      offset: 0
    - length: 5
      offset: 1
    - length: 7
      offset: 6
    - length: 13
      offset: 13
    - length: 1
      offset: 26

所以改变

my @tagobj_header_table;
push @tagobj_header_table, make_tagobj_core_header;
push @tagobj_header_table, make_tagobj_text_header;

my $dumper = YAML::Dumper->new;
print $dumper->dump(@tagobj_header_table);

my %doc = (
   %{ make_tagobj_core_header() },
   %{ make_tagobj_text_header() },
);

print YAML::Dumper->new->dump(\%doc);

虽然我认为最好更改潜艇,以便以下工作:

my %doc = (
   core => make_tagobj_core_header(),
   text => make_tagobj_text_header(),
);

print YAML::Dumper->new->dump(\%doc);