开发者

Why is this hash not displaying correctly?

after being sick for a few week I am开发者_Go百科 trying to get back into my scripting projects and seems to be running into a newbie speed bump.

I am trying to assemble a script to slurp a file and then process parameters from the file using regex and build a hash from parameters found.

But the problem I am running into is the hash is not being constructed the way I want it, or atleast I think it is not.

Here is the tiny script I am working on.

#!/usr/bin/perl
use strict;
use warnings;
use File::Slurp;
use Data::Dumper;

my %config;

my $text = read_file("./config/settings.cfg");

if ($text =~ /^esxi\.host\s+=\s+(?<host>.+)/xm) {
    $config{host} = "$+{host}";
}

print Dumper (%config);

For those wishing to execute the script here is the config file I am building

Connection Options:
######################################################
esxi.host = server01
esxi.port = 22
esxi.username = root
esxi.password = password
######################################################

Backup Options:
#########################
Compression Options:
0 = none
1 = tar
2 = gzip
3 = tar+gzip
#########################
backup.compression = 0

Just save it to a file called settings.cfg unless you feel like changing the parameter in the script.

Anyhow this is the output I am getting from Data::Dumper.

$VAR1 = 'server01';
$VAR2 = {
          'host' => 'esxi01'
        };

What I am trying to do is make server01 the root key of the hash and host a subkey because I will also have subkeys for user, password, and port number.

I have been chewing on this for about a half hour (partly distracted) trying to figure out why it is not working, any help would be most appreciated.


Are you wanting output like this?

$VAR1 = {
      'server01' => {
                      'host' => 'esxi01'
                    }
    };

If so, your %config is fine. Your problem is you're passing a hash (which gets interpreted as an array a list) rather than a hashref to Dumper. Try Dumper(\%config) instead.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜