YAML :: Syck生成列表而不是哈希

时间:2023-01-15 00:08:03

If I run the below script, then the two hashes are the same, but when I look at how the yaml file looks I see this very weird format

如果我运行下面的脚本,那么两个哈希是相同的,但当我看看yaml文件的外观时,我看到这种非常奇怪的格式

--- nicPri
--- ixgbe1
--- nicPub
--- ixgbe2
--- slaves
--- 
47: 10.10.47.47
48: 10.10.10.48
--- ipPri
--- 10.10.10.46

which doesn't look like a normal yaml file. I would have expected something like this

这看起来不像普通的yaml文件。我本以期待这样的事情

---
users:
  abc: abc
  ssd: www
01102:
  members:
    - abc
    - ssd
---

Question

Can anyone see why I this this weird yaml format?

任何人都可以看到为什么我这个奇怪的yaml格式?

#!/usr/bin/perl
use strict;
use YAML::Syck;
use warnings;
use Data::Dumper;

our %c = (
    slaves => {
        "47" => "10.10.47.47",
        "48" => "10.10.10.48",
    },
    ipPri         => "10.10.10.46",
    nicPub        => "ixgbe2",
    nicPri        => "ixgbe1",
);

our $config = "/tmp/config.yaml";

# create new yaml file
system("rm -f $config");
open F, '>', $config;
print F YAML::Syck::Dump(%c);
close F;
my %cfg = YAML::Syck::LoadFile($config);

print Dumper \%cfg;
print Dumper \%c;

1 个解决方案

#1


Answer

YAML::Syck (as YAML) expects hashref, not hash:

YAML :: Syck(作为YAML)期望hashref,而不是hash:

print F YAML::Syck::Dump(\%c);

Also LoadFile returns hashref, not hash:

另外,LoadFile返回hashref,而不是hash:

my $cfg = YAML::Syck::LoadFile($config);    
print Dumper $cfg;

Other improvements

First of all, you don't really need our here. Your %c means pretty much the same as %main::c. our creates the alias for the package variable. And this is not what you want. You should just use regular scoped variables via my, e. g. my %c.

首先,你真的不需要我们这里。你的%c与%main :: c几乎相同。我们为包变量创建别名。这不是你想要的。你应该通过我的e使用常规的范围变量。 G。我的C。

Using open with filehandle is not considered a good practice anymore, use scalar instead:

使用open with filehandle不再被认为是一种好习惯,而是使用标量:

open(my $fh, '>', $config);

Also you can use $fh->print instead of print $fh. If you still prefer to use print $fh, you should use print {$fh} instead (according to Conway).

您也可以使用$ fh-> print而不是print $ fh。如果你仍然喜欢使用print $ fh,你应该使用print {$ fh}代替(根据Conway)。

#1


Answer

YAML::Syck (as YAML) expects hashref, not hash:

YAML :: Syck(作为YAML)期望hashref,而不是hash:

print F YAML::Syck::Dump(\%c);

Also LoadFile returns hashref, not hash:

另外,LoadFile返回hashref,而不是hash:

my $cfg = YAML::Syck::LoadFile($config);    
print Dumper $cfg;

Other improvements

First of all, you don't really need our here. Your %c means pretty much the same as %main::c. our creates the alias for the package variable. And this is not what you want. You should just use regular scoped variables via my, e. g. my %c.

首先,你真的不需要我们这里。你的%c与%main :: c几乎相同。我们为包变量创建别名。这不是你想要的。你应该通过我的e使用常规的范围变量。 G。我的C。

Using open with filehandle is not considered a good practice anymore, use scalar instead:

使用open with filehandle不再被认为是一种好习惯,而是使用标量:

open(my $fh, '>', $config);

Also you can use $fh->print instead of print $fh. If you still prefer to use print $fh, you should use print {$fh} instead (according to Conway).

您也可以使用$ fh-> print而不是print $ fh。如果你仍然喜欢使用print $ fh,你应该使用print {$ fh}代替(根据Conway)。