Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight

Loading multiple files with YAML::XS

by walkingthecow (Friar)
on Aug 04, 2013 at 09:46 UTC ( #1047781=perlquestion: print w/replies, xml ) Need Help??
walkingthecow has asked for the wisdom of the Perl Monks concerning the following question:

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.

Replies are listed 'Best First'.
Re: Loading multiple files with YAML::XS
by Perlbotics (Chancellor) on Aug 04, 2013 at 11:26 UTC

    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;


    # 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.

Re: Loading multiple files with YAML::XS (named streams)
by Anonymous Monk on Aug 05, 2013 at 07:42 UTC
      Hi,, can u provide me an example or appropriate link in which two configs files are loaded using YAML in perl

        You might want to look at Config::Any.

        From the docs:

        load_files() attempts to load configuration from the list of files pas +sed in the files parameter, if the file exists.
        The module recognizes YAML files and will parse them with the appropriate YAML library (which means you have to have it installed).


        The way forward always starts with a minimal test.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1047781]
Approved by Perlbotics
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others chilling in the Monastery: (8)
As of 2018-06-24 02:42 GMT
Find Nodes?
    Voting Booth?
    Should cpanminus be part of the standard Perl release?

    Results (126 votes). Check out past polls.