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; my $data = LoadFile('file1.yml'); $data = LoadFile('file2.yml');
But, I am not sure if it is the best way to do it.#!/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 }; }
And my code looks like so:--- name: file1 --- name: file2
The output using YAML::XS looks like this:#!/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);
Oddly, using YAML::Tiny with the following code:$VAR1 = { 'name' => 'file2' };
I get the output I would expect:#!/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);
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 = bless( [ { 'name' => 'file1' }, { 'name' => 'file2' } ], 'YAML::Tiny' );
I'm confused. What behavior should be expected here? I would think YAML::Tiny's, but I could very well be wrong.$VAR1 = { 'name' => 'file1' };
In reply to Loading multiple files with YAML::XS by walkingthecow
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |