Jesse Lactin
Jesse Lactin

Reputation: 311

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. Here's the Perl code I'm using to build a YAML document and serialize it.

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.

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

Upvotes: 0

Views: 254

Answers (2)

Borodin
Borodin

Reputation: 126722

The Perl data structure you are using doesn't correspond to the YAML data that you require. 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. 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::XS is the preferred Perl implementation of YAML, and is a binding for the excellent libyaml library

Upvotes: 2

badp
badp

Reputation: 11813

This isn't a YAML problem; lines should map to an arrayref, not a 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,
     }
     }
  }
};

Upvotes: 0

Related Questions