Looking of a value in blessed hash - perl

I am a begginer in Perl an i am trying to get à value from à blessed hash.
The value is ip adresses, i tried that with no success
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' )
]

Whatever you have dumped is an array, not a hash. You need to show the call to Dumper for us to help you properly
Also, since this is a structure of blessed objects, you should be using their methods to access information, not going by the "back door" and messing with the data structure directly. Unfortunately GuestStackInfo and NetDnsConfigInfo are VMware classes and not one of the standard Perl types so I can't suggest what method calls may be appropriate
Here are some notes
The structure referred to by $VAR1 is a one-element array containing a GuestStackInfo object
The GuestStackInfo object contains a NetIpRouteConfigInfo object and a NetDnsConfigInfo object. I assume you are interested in the latter as you say "The value is ip adresses", and the nearest hash key is ipAddress in the NetDnsConfigInfo object
The ipAddress element is reference to an array of IP address-like strings
To access this array you would write
my $addresses = $VAR1->[0]{dnsConfig}{ipAddress};
and then to print them all out, use
print "$_\n" for #$addresses;
But please take note of my initial comments -- you should be using method calls and not poking around the data structure like this. Is there any documentation for those classes?

Related

How to recreate perl's array of hashes in golang?

In perl I can push $hashref into #array and have this data for next foreach and possible encode_json (HTTP POST).
I can't figure out how to recreate the same login in golang?
$VAR1 = [
{
'address' => 'test.com',
'id' => 101,
'hostgroups' => [
zero
'one',
'or many'
],
'host_name' => 'test.com',
'alias' => 'test.com',
'template' => 'generic',
'file_id' => 'etc/config'
},
{
'address' => 'test2.com',
'id' => 102,
'hostgroups' => [
zero
'one',
'or many'
],
'host_name' => 'test2.com',
'alias' => 'test2.com',
'template' => 'generic',
'file_id' => 'etc/config'
},
(..)
var array = []map[string]interface{}{
{"address": "test.com", "hostgroups": []string{"zero", "one", "or many"}, "id": 101},
{"address": "test2.com", "hostgroups": []string{"zero", "one", "or many"}, "id": 102},
}
This is the answer.
type host map[string]interface{}
var hosts []host
h := host{
"id": id,
"file_id": "etc/config/hosts.cfg",
"host_name": host_name,
"alias": host_name,
"address": host_name,
"hostgroups": hg,
"template": "generic-host",
}
hosts = append(hosts, h)

Append an element to an already existing SOAP::Data complex type

I'm very new to SOAP, PERL and pretty much everything else I've been asked to do so I'm hoping someone can point me in the right direction.
I've implemented a simple WCF solution and I've written a PERL client which passes a "complex data structure" to the solution using SOAP::lite and SOAP::Data. All this works very well so far, WCF solution see's the array as an array and I'm able to iterate through the array on the server side just fine.
However, I'm having an issue trying to append a data element to the array on the PERL side. I have the following code, which builds the array I need, but I need to append a few lines to the array later on in the code and I can't figure out how to that.
# build array of values
my $data= SOAP::Data->new
(name => 'array', value =>
[
SOAP::Data->new(name => 'elem:string', value => 'firststring'),
SOAP::Data->new(name => 'elem:string', value => 'secondstring'),
SOAP::Data->new(name => 'elem:string', value => 'thridstring')
]
)
->attr
(
{ 'xmlns:elem' => 'http://schemas.microsoft.com/2003/10/Serialization/Arrays','xmlns:i' => 'http://www.w3.org/2001/XMLSchema-instance'}
);
# create a new element
my $elem1 = SOAP::Data->new(name => 'elem:string', value => 'addedstring');
# try to add the element
push(#{$data->{array}},$elem1);
#.... send, catch, print.. bla bla bla
The code I have runs, and the WCF service see's the array just fine, but the $elem1 value is never actually appended to the SOAP envelope.
Any help is GREATLY appreciated...
Take a look at what $data is using Data::Dumper, you get this
$VAR1 = bless( {
'_attr' => {
'xmlns:i' => 'http://www.w3.org/2001/XMLSchema-instance',
'xmlns:elem' => 'http://schemas.microsoft.com/2003/10/Serialization/Arrays'
},
'_signature' => [],
'_name' => 'array',
'_value' => [
[
bless( {
'_value' => [
'firststring'
],
'_name' => 'string',
'_prefix' => 'elem',
'_signature' => [],
'_attr' => {}
}, 'SOAP::Data' ),
bless( {
'_value' => [
'secondstring'
],
'_name' => 'string',
'_signature' => [],
'_prefix' => 'elem',
'_attr' => {}
}, 'SOAP::Data' ),
bless( {
'_attr' => {},
'_value' => [
'thridstring'
],
'_name' => 'string',
'_signature' => [],
'_prefix' => 'elem'
}, 'SOAP::Data' )
]
]
}, 'SOAP::Data' );
There is no $data->{array}
A look at the documentation for SOAP::Data, says you should use $data->value to access the array you created.
push #{ $data->value }, $elem1;
print Dumper $data->value;
yields
$VAR1 = [
bless( {
'_attr' => {},
'_prefix' => 'elem',
'_value' => [
'firststring'
],
'_name' => 'string',
'_signature' => []
}, 'SOAP::Data' ),
bless( {
'_signature' => [],
'_name' => 'string',
'_value' => [
'secondstring'
],
'_prefix' => 'elem',
'_attr' => {}
}, 'SOAP::Data' ),
bless( {
'_name' => 'string',
'_signature' => [],
'_value' => [
'thridstring'
],
'_prefix' => 'elem',
'_attr' => {}
}, 'SOAP::Data' ),
bless( {
'_attr' => {},
'_prefix' => 'elem',
'_value' => [
'addedstring'
],
'_name' => 'string',
'_signature' => []
}, 'SOAP::Data' )
];
Thanks Gabs00,
push $data->value, $elem1; worked beautifully

Stuck trying to access hash value

I inherited a script and I need to be able to access some data from a hash. I want to be able to access the MB_Path value from the following.
$VAR1 = bless(
{
'ME_Parts' => [
bless(
{
'ME_Bodyhandle' => bless(
{
'MB_Path' => '/tmp/msg-15072-1.txt'
},
'MIME::Body::File'
),
'ME_Parts' => [],
'mail_inet_head' => bless(
{
'mail_hdr_foldlen' => 79,
'mail_hdr_modify' => 0,
'mail_hdr_list' => [
'Content-Type: text/plain; charset="us-ascii"',
'Content-Transfer-Encoding: quoted-printable'
],
'mail_hdr_hash' => {
'Content-Type' => [
\$VAR1->{'ME_Parts'}[0]{'mail_inet_head'}
{'mail_hdr_list'}[0]
],
'Content-Transfer-Encoding' => [
\$VAR1->{'ME_Parts'}[0]{'mail_inet_head'}
{'mail_hdr_list'}[1]
]
},
'mail_hdr_mail_from' => 'KEEP',
'mail_hdr_lengths' => {}
},
'MIME::Head'
)
},
'MIME::Entity'
),
bless(
{
'ME_Bodyhandle' => bless(
{
'MB_Path' => '/tmp/msg-15072-2.html'
},
'MIME::Body::File'
),
'ME_Parts' => [],
'mail_inet_head' => bless(
{
'mail_hdr_foldlen' => 79,
'mail_hdr_modify' => 0,
'mail_hdr_list' => [
'Content-Type: text/html;charset="us-ascii"',
'Content-Transfer-Encoding: quoted-printable'
],
'mail_hdr_hash' => {
'Content-Type' => [
\$VAR1->{'ME_Parts'}[1]{'mail_inet_head'}
{'mail_hdr_list'}[0]
],
'Content-Transfer-Encoding' => [
\$VAR1->{'ME_Parts'}[1]{'mail_inet_head'}
{'mail_hdr_list'}[1]
]
},
'mail_hdr_mail_from' => 'KEEP',
'mail_hdr_lengths' => {}
},
'MIME::Head'
)
},
'MIME::Entity'
)
],
'ME_Epilogue' => [],
'ME_Preamble' => [],
'mail_inet_head' => bless(
{
'mail_hdr_foldlen' => 79,
'mail_hdr_modify' => 0,
'mail_hdr_list' => [
'Content-Type: multipart/alternative;boundary="----_=_NextPart_002_01CEB949.DC6B0180"'
],
'mail_hdr_hash' => {
'Content-Type' =>
[ \$VAR1->{'mail_inet_head'}{'mail_hdr_list'}[0] ]
},
'mail_hdr_mail_from' => 'KEEP',
'mail_hdr_lengths' => {}
},
'MIME::Head'
)
'MIME::Entity'
);
I thought I could simply do the following
print $ent->parts->($i)->{ME_Bodyhandle}->{MB_Path};
However when I do that I get and error that the value is not initialized. But when I do dump of just $ent->parts->($i) I get the above code.
I am just stuck on this one.
Thanks,
Leo C
You don't have a hash, you have an object (which happens to be implemented as a hash). That's why the Data::Dumper output keeps saying bless(...). You shouldn't be poking into its internals.
I think you're looking for
$ent->parts($i)->bodyhandle->path;
Until you have exhausted the possibilities of the documentation, there is no excuse for dumping the underlying data structure that represents a Perl object and hard-coding access to its components. The rules of encapsulation apply to Perl object-oriented programming just as much as any other language.
The documentation for
MIME::Entity
and
MIME::Body
is quite clear, and the code you need is something like this
for my $part ($ent->parts) {
my $path = $part->bodyhandle->path;
print $path, "\n";
}
output
/tmp/msg-15072-1.txt
/tmp/msg-15072-2.html
This:
print $ent->parts->($i)->{ME_Parts}->[$i]->{ME_Bodyhandle}->{MB_Path};

LWP::UserAgent and proxy error

I have a proxy error when using LWP::UserAgent
this is the code:
my $ua = LWP::UserAgent->new();
$ua->proxy( http => $ENV{HTTP_PROXY});
print Dumper($ua);
my $request = new HTTP::Request('GET', $link);
print Dumper( $request );
and this is the dumper for UserAgent
$VAR1 = bless( {
'max_redirect' => 7,
'protocols_forbidden' => undef,
'show_progress' => undef,
'handlers' => {
'response_header' => bless( [
{
'owner' => 'LWP::UserAgent::parse_head',
'callback' => sub { "DUMMY" },
'm_media_type' => 'html',
'line' => 'C:/Perl/lib/LWP/UserAgent.pm:612'
}
], 'HTTP::Config' ),
'request_preprepare' => bless( [
{
'owner' => 'LWP::UserAgent::proxy',
'callback' => sub { "DUMMY" },
'line' => 'C:/Perl/lib/LWP/UserAgent.pm:920'
}
], 'HTTP::Config' )
},
'no_proxy' => [],
'protocols_allowed' => undef,
'local_address' => undef,
'use_eval' => 1,
'requests_redirectable' => [
'GET',
'HEAD'
],
'timeout' => 90,
'def_headers' => bless( {
'user-agent' => 'libwww-perl/5.837'
}, 'HTTP::Headers' ),
'proxy' => {
'http' => 'http://igate:8080'
},
'max_size' => undef
}, 'LWP::UserAgent' );
And this is for the request:
$VAR1 = bless( {
'_content' => '',
'_uri' => bless( do{\(my $o = 'https://some_link')}, 'URI::https' ),
'_headers' => bless( {}, 'HTTP::Headers' ),
'_method' => 'GET'
}, 'HTTP::Request' );
the problem is that the response is an error:
FAIL response, 500 proxy connect failed: PROXY ERROR HEADER, could be non-SSL URL:
HTTP/1.1 503 Service Unavailable
I'm using ActiveState perl 5.10.1 on a WinXP machine
when accessing the link from browser it work
Can somebody help?
Thanks
I've always needed to set https_proxy (rather than just http_proxy) to work with SSL URIs.

Perl printing second level hash keys in a nested hash

How do I print all my second level hash keys (sig_qtr, date, range, etc.) given a hash like such:
my $xml = XMLin("./${spec_file}", ForceArray => ['range', 'constant', 'question', 'date', 'sig_yr', 'sig_qtr', 'sig_mth'], KeyAttr => {});
print Dumper $xml->{entities};
print dumper output of hash:
$VAR1 = {
'sig_qtr' => [
{
'name' => 'q1',
'label' => 'q1'
},
{
'name' => 'q4',
'label' => 'q4'
}
],
'date' => [
{
'name' => 'y2_mth',
'label' => 'pryr_mth_curr'
},
{
'name' => 'y3_pod6_qtr4',
'label' => 'curr_qtd4'
}
],
'range' => [
{
'name' => 'y0_jun',
'end' => '20100631',
'start' => '20100601'
},
{
'name' => 'y3_oct',
'end' => '20131031',
'start' => '20131001'
}
],
'constant' => [
{
'spec' => '99999999 and 99999999',
'name' => 'none_sixmth'
}
],
'sig_yr' => [
{
'name' => 'y1_sig',
'label' => 'ye11'
},
{
'name' => 'y3_sig',
'label' => 'ytd'
}
],
'sig_mth' => [
{
'name' => 'y3_nov',
'label' => 'nov12'
},
{
'name' => 'y3_oct',
'label' => 'oct13'
}
],
'question' => [
{
'name' => 'ltrq',
'label' => 'q9'
},
{
'name' => 'nextprod',
'label' => 'q12a'
}
],
'backfill' => {
'label' => 'bf_period'
},
'year' => {
'current' => '2013'
}
};
would be even better if keys are put into an array.
Thanks.
print "$_\n" for keys %{ $xml->entities };
To put them into an array,
my #keys = keys %{ $xml->entities };