Yes, you are confused ;-)
First, no YAML module will realise your unspecified merge behaviour other than incidentally.
Second, two ore more YAML documents in a single file are not merged but do have the representation of a list (FIFO).

It seems you want a HoHoH where the key of the first hash is the same as value of the hash addressed by the key name in the second level hash? No YAML module will do this kind of merging for you. So the approach you have shown in your second snipped is the way to go.

Now the confusion:

my $data = LoadFile( $file );
is not the same as
my ($data) = LoadFile( $file);
if the YAML file contains more than one document (note the parenthesis). Problem is Perl syntax here, not differences in module behaviour. See also: perlfaq4.

use strict; use warnings; use Test::More; use Test::Deep; use YAML::Tiny (); use YAML::XS (); my $yaml_doc =<<YAML_DOC; --- name: file1 item: x --- name: file2 item: y YAML_DOC note "YAML::XS::VERSION = $YAML::XS::VERSION"; note "YAML::Tiny::VERSION = $YAML::Tiny::VERSION"; my $xs_list = [ YAML::XS::Load ( $yaml_doc ) ]; my $tiny_list = [ YAML::Tiny::Load( $yaml_doc ) ]; is( @{ $xs_list }, 2, "YAML::XS - two documents in a single fil +e" ); is( @{ $tiny_list }, 2, "YAML::Tiny - two documents in a single fil +e" ); cmp_deeply( $xs_list, $tiny_list, "multi documents are identical" ); my $scalar1_xs = YAML::XS::Load( $yaml_doc ); my ($scalar2_xs) = YAML::XS::Load( $yaml_doc ); my $scalar1_ty = YAML::Tiny::Load( $yaml_doc ); my ($scalar2_ty) = YAML::Tiny::Load( $yaml_doc ); cmp_deeply( $scalar1_xs, $scalar1_ty, "same scalar/list behaviour"); +#-- Obvious! cmp_deeply( $scalar2_xs, $scalar2_ty, "same scalar/list behaviour"); +# We test Perl syntax here. note 'Here is your problem: ( ... )'; note 'my $scalar1_xs ... last from list (like comma operator):'; note explain $scalar1_xs; note 'my ($scalar2_xs) ... with pars - first from list:'; note explain $scalar2_xs; done_testing;

Result:

# YAML::XS::VERSION = 0.41 # YAML::Tiny::VERSION = 1.51 ok 1 - YAML::XS - two documents in a single file ok 2 - YAML::Tiny - two documents in a single file ok 3 - multi documents are identical ok 4 - same scalar/list behaviour ok 5 - same scalar/list behaviour # Here is your problem: ( ... ) # my $scalar1_xs ... last from list (like comma operator): # { # 'item' => 'y', # 'name' => 'file2' # } # my ($scalar2_xs) ... with pars - first from list: # { # 'item' => 'x', # 'name' => 'file1' # } 1..5

So, for the given versions (and the limited amount of tests given here), YAML::XS and YAML::Tiny behave the same.


In reply to Re: Loading multiple files with YAML::XS by Perlbotics
in thread 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.