I am trying to do something like this:
#!/usr/bin/env perl use strict; use warnings; use YAML::XS; my $data = LoadFile('file1.yml'); $data = LoadFile('file2.yml');
I'd like for $data to contain the YAML structure from both file1.yml and file2.yml. Not sure how to do this though. I can accomplish it like this:
#!/usr/bin/env perl use strict; use warnings; use YAML::XS qw(LoadFile); my @files = qw( /tmp/file1.yml /tmp/file2.yml ); my $test = { }; foreach my $file ( @files ) { my $data = LoadFile($file); my $name = $data->{name}; $test->{$name} = { %$data }; }
But, I am not sure if it is the best way to do it.

Also, I was under the impression that YAML::XS could load two different documents. So, I have a YAML file like this:
--- name: file1 --- name: file2
And my code looks like so:
#!/usr/bin/env perl use strict; use warnings; use YAML::XS qw(LoadFile); use Data::Dumper; my $data = LoadFile( '/tmp/test.yml' ); print Dumper($data);
The output using YAML::XS looks like this:
$VAR1 = { 'name' => 'file2' };
Oddly, using YAML::Tiny with the following code:
#!/usr/bin/env perl use strict; use warnings; use YAML::Tiny; use Data::Dumper; my $data = YAML::Tiny->read('/tmp/test.yml'); print Dumper($data);
I get the output I would expect:
$VAR1 = bless( [ { 'name' => 'file1' }, { 'name' => 'file2' } ], 'YAML::Tiny' );
YAML::Syck has the opposite problem that YAML::XS shows (it doesn't load the next document, so I am left with the first document):
$VAR1 = { 'name' => 'file1' };
I'm confused. What behavior should be expected here? I would think YAML::Tiny's, but I could very well be wrong.

In reply to Loading multiple files with YAML::XS by walkingthecow

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.