walkingthecow has asked for the wisdom of the Perl Monks concerning the following question:
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' };
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Loading multiple files with YAML::XS
by Perlbotics (Archbishop) on Aug 04, 2013 at 11:26 UTC | |
|
Re: Loading multiple files with YAML::XS (named streams)
by Anonymous Monk on Aug 05, 2013 at 07:42 UTC | |
by Lunaa (Initiate) on Dec 02, 2015 at 10:19 UTC | |
by 1nickt (Canon) on Dec 02, 2015 at 15:00 UTC |