yml文件合二为一
yml files to one
我打算使用“use Hash::Merge qw( merge );
”
将两个 yml 文件合并为一个 yml 文件
这是我的示例脚本:-
use strict;
use warnings;
use Hash::Merge qw( merge );
use YAML::XS qw(Load Dump);
use File::Slurp qw(read_file write_file append_file);
my $yaml1 = "/home/test/one.yml";
my $yaml2 = "/home/test/two.yml";
my $in_yaml = read_file($yaml1, { binmode => ':raw' });
my $clkgrps = Load $in_yaml ;
my $in_yaml1 = read_file($yaml2, { binmode => ':raw' });
my $clkgrps1 = Load $in_yaml1;
my $clockgroups = merge($clkgrps, $clkgrps1);
my $out_yaml = Dump $clockgroups;
my $outFile = "clockgroups_used.yml" ;
print("Generating Yaml file: $outFile \n") ;
write_file($outFile, { binmode => ':raw' }, $out_yaml);
one.yml:-
emailName: David
emailAddresses:
- sillymoos@cpan.org
- perltricks.com@gmail.com
credentials:
username: sillymoose
password: itsasecret
two.yml:-
emailName: Pranay
emailAddresses:
- heelo@cpan.org
- test@gmail.com
credentials:
username: link
password: sanity
执行我的脚本后,我确实看到了最终 yml 文件的输出 clockgroups_used.yml 包含以下内容,这些内容仅来自 one.yml
---
credentials:
password: itsasecret
username: sillymoose
emailAddresses:
- sillymoos@cpan.org
- perltricks.com@gmail.com
- heelo@cpan.org
- test@gmail.com
emailName: David
我是不是做错了什么?为什么我看不到两个 yml 文件的合并?
加载Hash::Merge
模块后,添加这一行:
Hash::Merge::set_behavior('RETAINMENT_PRECEDENT');
Hash::Merge
的默认行为是不更改散列中元素的数据类型,并优先考虑 merge
的第一个参数的内容。设置 RETAINMENT_PRECEDENT
指示 Hash::Merge
通过在必要时创建数组来保留两个哈希的所有数据。
因此生成的 YAML 文件是
---
credentials:
password:
- itsasecret
- sanity
username:
- sillymoose
- link
emailAddresses:
- sillymoos@cpan.org
- perltricks.com@gmail.com
- heelo@cpan.org
- test@gmail.com
emailName:
- David
- Pranay
我打算使用“use Hash::Merge qw( merge );
”
这是我的示例脚本:-
use strict;
use warnings;
use Hash::Merge qw( merge );
use YAML::XS qw(Load Dump);
use File::Slurp qw(read_file write_file append_file);
my $yaml1 = "/home/test/one.yml";
my $yaml2 = "/home/test/two.yml";
my $in_yaml = read_file($yaml1, { binmode => ':raw' });
my $clkgrps = Load $in_yaml ;
my $in_yaml1 = read_file($yaml2, { binmode => ':raw' });
my $clkgrps1 = Load $in_yaml1;
my $clockgroups = merge($clkgrps, $clkgrps1);
my $out_yaml = Dump $clockgroups;
my $outFile = "clockgroups_used.yml" ;
print("Generating Yaml file: $outFile \n") ;
write_file($outFile, { binmode => ':raw' }, $out_yaml);
one.yml:-
emailName: David
emailAddresses:
- sillymoos@cpan.org
- perltricks.com@gmail.com
credentials:
username: sillymoose
password: itsasecret
two.yml:-
emailName: Pranay
emailAddresses:
- heelo@cpan.org
- test@gmail.com
credentials:
username: link
password: sanity
执行我的脚本后,我确实看到了最终 yml 文件的输出 clockgroups_used.yml 包含以下内容,这些内容仅来自 one.yml
---
credentials:
password: itsasecret
username: sillymoose
emailAddresses:
- sillymoos@cpan.org
- perltricks.com@gmail.com
- heelo@cpan.org
- test@gmail.com
emailName: David
我是不是做错了什么?为什么我看不到两个 yml 文件的合并?
加载Hash::Merge
模块后,添加这一行:
Hash::Merge::set_behavior('RETAINMENT_PRECEDENT');
Hash::Merge
的默认行为是不更改散列中元素的数据类型,并优先考虑 merge
的第一个参数的内容。设置 RETAINMENT_PRECEDENT
指示 Hash::Merge
通过在必要时创建数组来保留两个哈希的所有数据。
因此生成的 YAML 文件是
---
credentials:
password:
- itsasecret
- sanity
username:
- sillymoose
- link
emailAddresses:
- sillymoos@cpan.org
- perltricks.com@gmail.com
- heelo@cpan.org
- test@gmail.com
emailName:
- David
- Pranay