简体   繁体   English

如何在Perl中使用YAML加载多个文件

[英]How to load multiple files in using YAML in perl

i need to load two different config files in perl using YAML, 我需要使用YAML在perl中加载两个不同的配置文件,

use YAML qw'LoadFile'; 使用YAML qw'LoadFile';

then in first function i used 然后在我使用的第一个功能中

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

and in 2nd one i used 在第二个我用

my $conf = LoadFile( '/config/XYZ.yaml'); 我的$ conf = LoadFile('/config/XYZ.yaml'); my $key = $conf->{xyz}; 我的$ 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. 恐怕您没有提供足够多的信息来帮助我们诊断问题,但这是根据您的要求加载两个不同的YAML文件的演示。 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.yaml

---
test: value for test

XYZ.yaml XYZ.yaml

---
xyz: value for xyz

test.pl 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. 因此,我更改了Borodin答案,以更好地反映我认为您在做什么。

#!/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. 如果您需要更多帮助,则需要向我们提供更多详细信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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