简体   繁体   中英

How to load multiple files in using YAML in perl

i need to load two different config files in perl using YAML,

use YAML qw'LoadFile';

then in first function i used

my $conf = LoadFile('/config/test.yaml'); my $serve = $conf->{test};

and in 2nd one i used

my $conf = LoadFile( '/config/XYZ.yaml'); my $key = $conf->{xyz};

now the in this case if i used only one file then its works fine, but used them simultaneously gives me error. Do anyone know its reason?

I'm afraid you haven't given anywhere near enough information for us to diagnose your problem, but here's a demonstration of loading two different YAML files, as you asked. As you can see, it's pretty much identical to what you have shown of your own code, which should work fine

test.yaml

---
test: value for test

XYZ.yaml

---
xyz: value for xyz

test.pl

use strict;
use warnings 'all';
use feature 'say';

use YAML qw/ LoadFile /;

my $conf = LoadFile('test.yaml');
say $conf->{test};

$conf = LoadFile('XYZ.yaml');
say $conf->{xyz};

output

value for test
value for xyz

I noticed that in your question, you talked about loading the two files in different functions. So I altered Borodin 's answer to better reflect what I think you are doing.

#!/usr/bin/perl

use strict;
use warnings 'all';
use feature 'say';

use YAML qw/ LoadFile /;

sub load_test {
  my $conf = LoadFile('test.yaml');
  my $test = $conf->{test};
  say $test;
}

sub load_xyz {
  my $conf = LoadFile('XYZ.yaml');
  my $xyz = $conf->{xyz};
  say $xyz;
}

load_test();
load_xyz();

When I run that, I get:

value for test
value for xyz

So I can't see what the problem is. If you want more help then you are going to need to give us a lot more detail.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM