【问题标题】:Looking of a value in blessed hash在祝福哈希中查找值
【发布时间】:2016-04-07 23:44:54
【问题描述】:

我是 Perl 的初学者,我正在尝试从 à 祝福哈希中获得 à 值。

值是ip地址,我试过没有成功

print $vm->guest->ipStack->dnsConfig->ipAddress;

print $vm->guest->ipStack{dnsConfig}{ipAddress};


$VAR1 = [
    bless( {

        "ipRouteConfig" => bless( {

            "ipRoute" => [

                bless( {
                    "gateway" => bless( {
                        "device" => 0,
                        "ipAddress" => "10.*******"
                    }, 'NetIpRouteConfigInfoGateway' ),
                    "network" => "0.0.0.0",
                    "prefixLength" => 0
                }, 'NetIpRouteConfigInfoIpRoute' ),

                bless( {
                    "network" => "1***********",
                    "gateway" => bless( {
                        "device" => 0
                    }, 'NetIpRouteConfigInfoGateway' ),
                    "prefixLength" => 23
                }, 'NetIpRouteConfigInfoIpRoute' ),

                bless( {
                    "prefixLength" => 32,
                    "network" => "10**************",
                    "gateway" => bless( {
                        "device" => 0
                    }, 'NetIpRouteConfigInfoGateway' )
                }, 'NetIpRouteConfigInfoIpRoute' ),

                bless( {
                    "prefixLength" => 32,
                    "gateway" => bless( {
                        "device" => 0
                    }, 'NetIpRouteConfigInfoGateway' ),
                    "network" => "1***********5"
                }, 'NetIpRouteConfigInfoIpRoute' ),

                bless( {
                    "prefixLength" => 4,
                    "gateway" => bless( {
                        "device" => 0
                    }, 'NetIpRouteConfigInfoGateway' ),
                    "network" => "224.0.0.0"
                }, 'NetIpRouteConfigInfoIpRoute' ),

                bless( {
                    "gateway" => bless( {
                        "device" => 0
                    }, 'NetIpRouteConfigInfoGateway' ),
                    "network" => "255.255.255.255",
                    "prefixLength" => 32
                }, 'NetIpRouteConfigInfoIpRoute' ),

                bless( {
                    "prefixLength" => 64,
                    "network" => "fe80::",
                    "gateway" => bless( {
                        "device" => 0
                    }, 'NetIpRouteConfigInfoGateway' )
                }, 'NetIpRouteConfigInfoIpRoute' ),

                bless( {
                    "prefixLength" => 128,
                    "network" => "fe80::",
                    "gateway" => bless( {
                        "device" => 0
                    }, 'NetIpRouteConfigInfoGateway' )
                }, 'NetIpRouteConfigInfoIpRoute' ),

                bless( {
                    "prefixLength" => 8,
                    "network" => "ff00::",
                    "gateway" => bless( {
                        "device" => 0
                    }, 'NetIpRouteConfigInfoGateway' )
                }, 'NetIpRouteConfigInfoIpRoute' )
            ]

        }, 'NetIpRouteConfigInfo' ),

        "dnsConfig" => bless( {
            "dhcp" => 0,
            "searchDomain" => [
                "france"
            ],
            "hostName" => "HOST",
            "ipAddress" => [
                "10.60****",
                "10.6*****",
                "10.8*****"
            ],
            "domainName" => "france"
        }, 'NetDnsConfigInfo' )

    }, 'GuestStackInfo' )
]

【问题讨论】:

  • 这是什么版本的vmware sdk?你有文件吗?您能否请edit您的问题并添加产生 Data::Dumper 输出的代码。

标签: perl hash sdk vmware


【解决方案1】:

无论你转储的是一个数组,而不是一个哈希。您需要显示给Dumper的电话,以便我们为您提供适当的帮助

另外,由于这是一个祝福对象的结构,你应该使用他们的方法来访问信息,而不是走“后门”并搞乱直接数据结构。不幸的是,GuestStackInfoNetDnsConfigInfo 是 VMware 类,而不是标准 Perl 类型之一,所以我不能建议什么方法调用可能是合适的

这里有一些注释

  • $VAR1 引用的结构是一个包含GuestStackInfo 对象的单元素数组

  • GuestStackInfo 对象包含一个NetIpRouteConfigInfo 对象和一个NetDnsConfigInfo 对象。我假设您对后者感兴趣,因为您说 “值是 ip 地址”,并且最近的哈希键是 NetDnsConfigInfo 对象中的 ipAddress

  • ipAddress 元素是对类似 IP 地址的字符串的数组的引用

要访问这个数组,你会写

my $addresses = $VAR1->[0]{dnsConfig}{ipAddress};

然后将它们全部打印出来,使用

print "$_\n" for @$addresses;

但是请注意我最初的 cmets -- 你应该使用方法调用,而不是像这样在数据结构中闲逛。这些类是否有任何文档?

【讨论】:

  • 它有效,谢谢。我会寻找文档类。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-09-06
  • 2010-11-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-03-28
  • 2012-07-02
相关资源
最近更新 更多