Jesse Lactin
Jesse Lactin

Reputation: 311

Perl: Writing multiple headers to single YAML document

The following is the output YAML document I want.

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

Here's the code I'm using to create that YAML document to be serialized.

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

Here's the output.

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

Where did I go wrong? I think I need an array of hashes as my top-level entity in the YAML, but they get printed as separate documents separated by '---'. I would like to remove that without hacking on the output of YAML::Dumper::dump, as I have a use case for multiple identically structured YAML documents in a single file, which is supported by the spec.

Upvotes: 1

Views: 233

Answers (1)

ikegami
ikegami

Reputation: 385789

As you seem to know already,

a: b
c: d

is obtained by dumping a hash, so you want a hash as the top-level of your document. Proof of concept:

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 },
      ],
   },
});

Output:

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

So change

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

to

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

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

though I think it would be best if you change the subs so the following works:

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

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

Upvotes: 5

Related Questions