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:
is not the same asmy $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.my ($data) = LoadFile( $file);
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
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |