in reply to Keeping Order with YAML::XS

Since you know what you're aiming at, just use the yaml as a template. Load it into Perl and then dump the resulting data structure:

use YAML::XS qw(LoadFile); use Data::Dump qw(pp); pp LoadFile(\*DATA); __DATA__ path: /export/home/frank options: - param: i value: '1001' - param f - param: x value: eyes - flag: b value: toes - param: p arguments: - value: test
Produces:
{ arguments => [{ value => "test" }], options => [ { param => "i", value => 1001 }, "param f", { param => "x", value => "eyes" }, { flag => "b", value => "toes" }, { param => "p" }, ], path => "/export/home/frank", }

Which after looking back at your original post, is essentially the same as your input (which has a missing comma). So its not a matter of finding a data structure that produces your desired YAML. Maybe it's better to have asked why you want a specific format to your YAML? Isn't any format that properly encodes your data, okay?

Replies are listed 'Best First'.
Re^2: Keeping Order with YAML::XS
by walkingthecow (Friar) on Jul 29, 2013 at 05:46 UTC
    I can load a file without issues, and it creates what I am expecting. The issue comes when trying to dump it back. Here's an example:

    in.yaml
    path: /export/home/frank options: - param: i value: 1001 - param: f - param: x value: eyes arguments: - value: test
    Here's the Perl code:

    Perl Code
    #!/usr/bin/env perl use strict; use warnings; use YAML::XS qw(LoadFile DumpFile); use Data::Dumper; my $data = LoadFile('test.yaml'); print Dumper($data); DumpFile('out.yaml', $data);
    And here's the output yaml (out.yaml) that it creates:

    out.yaml
    --- arguments: - value: test options: - param: i value: 1001 - param: f - param: x value: eyes path: /export/home/frank
    Notice that in the input file the syntax was in such a way that it was an ordered sequence (from what I am understanding of YAML anyway). Yet, in the output it is no longer ordered at all. I am trying to figure out from a Perl data structure how to make an ordered YAML file like what is the input.
      Why do you say that the input is creating what you're expecting? The in.yaml you give above produces:
      { arguments => [{ value => "test" }], options => [ { param => "i", value => 1001 }, { param => "f" }, { param => "x", value => "eyes" }, ], path => "/export/home/frank", }

      IE. the top level items (arguments,options,path) are in a hash, which is an unordered set in Perl. You've lost your ordering on input, not on output. Unless you're talking about the options? In which case both formats are equivalent, at least to the YAML::XS loader which creates the same data structures in both cases.

      In what external context does the yaml produced by YAML::XS cause a problem? Because both formats generate the same data structures in Perl and therefore should be interchangeable without issue.

        Ahhh, yes! But, here's the thing. How do I create a data structure that is ordered. I mean, I realize that hashes are unordered, but I'd like to create an ordered YAML sequence from a data structure. The problem is, an ordered YAML sequence looks the same Perl hash structure wise (from input) as an unordered YAML on output. I need the YAML to be ordered, and my issue is creating the data structure that makes that.