perl - How do I use the data from a YAML file after reading it? -


i reading data in yaml file (use yaml qw/loadfile/). need able read values , insert them other files.

the yaml file in format:

---  host:   - name: first_host     interface:       - name: eth0         oldip: 1.2.3.4         newip: 2.3.4.5         oldgw: 1.2.3.1         newgw: 2.3.4.1       - name: eth1         oldip: 1.2.3.4         newip: 2.3.4.5         oldgw: 1.2.3.1         newgw: 2.3.4.1       - name: eth2         oldip: 1.2.3.4         newip: 2.3.4.5         oldgw: 1.2.3.1         newgw: 2.3.4.1 

if run through data::dumper following ($data::dumper::terse enabled):

{   'host' => [             {               'interface' => [                              {                                'oldgw' => '1.2.3.1',                                'newgw' => '2.3.4.1',                                'name' => 'eth0',                                'newip' => '2.3.4.5',                                'oldip' => '1.2.3.4'                              },                              {                                'oldgw' => '1.2.3.1',                                'newgw' => '2.3.4.1',                                'name' => 'eth1',                                'newip' => '2.3.4.5',                                'oldip' => '1.2.3.4'                              },                              {                                'oldgw' => '1.2.3.1',                                'newgw' => '2.3.4.1',                                'name' => 'eth2',                                'newip' => '2.3.4.5',                                'oldip' => '1.2.3.4'                              }                            ],               'name' => 'first_host'             },             ] } 

i need make changes, instance in /etc/sysconfig/network-scripts/ifcfg-eth0, swapping oldip value newip value. however, i'm coming short on how put use. if print value of loaded yaml file appears nothing more hash reference. but, if try dereference hash following:

reference found even-sized list expected 

this followed hash reference.

this script i'm starting with:

#!/usr/bin/perl  use strict; use warnings; use yaml qw(loadfile); use data::dumper; $data::dumper::terse = 1;  %data = loadfile("/home/user/bin/perl/dummy_data.yml");  print \%data 

can explain me need able read values input can make changes need make?

loadfile returning hashref, not hash. the difference subtle important.

you have option of using hashref is:

my $data = loadfile("data.yml"); print $data; 

or can cast hash:

my %data = %{ loadfile("data.yml") }; print %data; 

you can handle reference like, long know is.

you access elements little differently:

$data{'foo'}   #hash %data $data->{'foo'} #hashref $data 

you may notice subs tend expect hash references instead of hashes, sometimes. that's how people first come across them.


Comments

Popular posts from this blog

java - Run a .jar on Heroku -

java - Jtable duplicate Rows -

validation - How to pass paramaters like unix into windows batch file -