Home > other >  Could not write merged data into yml file
Could not write merged data into yml file

Time:09-22

I am trying to merge two yml files with following perl code. When I print the $res via Dumper module, I could see the data.

I ran my script, but final.yml has reference data, instead of original data:-

srcARRAY(0x1013320)local_pointerARRAY(0x10228f8)    

Code:-

use warnings;
use strict;
use feature 'say';
use Data::Dump;
use Data::Dumper;
use Storable qw(dclone);
use YAML qw(LoadFile);
use File::Slurp qw(write_file);
use Hash::Merge qw(merge);
Hash::Merge::set_behavior('RETAINMENT_PRECEDENT');

die "Usage: $0 file1 file2\n" if @ARGV != 2;
my ($fname1, $fname2) = @ARGV;

my $yml1 = LoadFile($fname1);
my $yml2 = LoadFile($fname2);


my $res = merge($yml1, $yml2);
#print Dumper $res;
write_file("final.yml", { binmode => ':raw' }, %{$res});

a.yml:

 local_pointer:
      - data:
          - dir: /release/p_analysis
            exe: bin/test.sh
            tool_name: power_hash
            type: exe
          - dir: /complete/release/1.2/55603074
            tool_name: axi_ip
            type: dir
       name: code_test.config        

    src:
      - dest: sw_config/
        ip: TOT
        name: defs.yml
      - dest: models/
        ip: TOT
        name: makebuild.vh

b.yml:

local_pointer:
      - data:
          - dir: /hw/debug/test/log/
            exe: log.pl
            tool_name: test1
            type: exe
          - dir: /releaase_test/p_analysis
            exe: bin/test1.sh
            tool_name: power1_hash
            type: exe
       name: code_test.config    

src:
  - dest: sw_configs/
    ip: TOT
    name: defs111.yml
  - dest: models/
    ip: TOT
    name: testbuild.vh
  

I was looking for:-

local_pointer:
    - data:
          - dir: /release/p_analysis
            exe: bin/test.sh
            tool_name: power_hash
            type: exe
          - dir: /complete/release/1.2/55603074
            tool_name: axi_ip
            type: dir
           - dir: /hw/debug/test/log/
            exe: log.pl
            tool_name: test1
            type: exe
          - dir: /releaase_test/p_analysis
            exe: bin/test1.sh
            tool_name: power1_hash
            type: exe
      name: code_test.config        
     
 src:
      - dest: sw_config/
        ip: TOT
        name: defs.yml
      - dest: models/
        ip: TOT
        name: makebuild.vh
      - dest: sw_configs/
        ip: TOT
        name: defs111.yml
      - dest: models/
        ip: TOT
        name: testbuild.vh

Should we need to set any custom settings in perl Hash::Merge module?

CodePudding user response:

To write the YAML data out to a file, use the YAML Dump function:

use warnings;
use strict;
use YAML qw(LoadFile Dump);
use File::Slurp qw(write_file);
use Hash::Merge qw(merge);
Hash::Merge::set_behavior('RETAINMENT_PRECEDENT');

die "Usage: $0 file1 file2\n" if @ARGV != 2;
my ($fname1, $fname2) = @ARGV;

my $yml1 = LoadFile($fname1);
my $yml2 = LoadFile($fname2);

my $res = merge($yml1, $yml2);
write_file("final.yml", Dump($res));

Or, you can also use YAML::DumpFile instead of File::Slurp::write_file:

DumpFile("final.yml", $res);
  • Related