natxo has asked for the wisdom of the Perl Monks concerning the following question:

hi, using the wmware Perl sdk I have this variable $var->{'config.datastoreUrl'} that when dumped looks like this:
$VAR1 = [ bless( { 'name' => 'Name', 'url' => '/vmfs/volumes/guidvalue' }, 'VirtualMachineConfigInfoDatastoreUrlPair' ) ]; $VAR2 = ' ';
I would like to get the value of 'name' and 'url', but I do not get the right syntax. Could you advise on how to do it? Thanks!

Replies are listed 'Best First'.
Re: help on dereferencing structure
by LanX (Saint) on Sep 10, 2014 at 19:43 UTC
    Its an array holding one object, which is a blessed hash.

    A "dirty" way to do it should be

    $var->{'config.datastoreUrl'}[0]{name}

    But since it's an object you should rather use a getter from its class VirtualMachineConfigInfoDatastoreUrlPair

    Something like (speculating about the right method)

    $var->{'config.datastoreUrl'}[0]->get_name()

    Cheers Rolf

    (addicted to the Perl Programming Language and ☆☆☆☆ :)

Re: help on dereferencing structure
by NetWallah (Canon) on Sep 10, 2014 at 23:43 UTC
    I would write this as: (UNTESTED)
    for my $datastore (@{ $var->{'config.datastoreUrl'} }){ print $datastore->{name}, " ", $datastore->{url},"\n"; }
    But a more official access method would be:
    # Assumes you have a $vm object. my $datastores = Vim::get_views(mo_ref_array => $vm->{'datastore'}, properties => ['name','url']); foreach (@$datastores) { print " " . $_->{'name'} . "\n"; }

            "You're only given one little spark of madness. You mustn't lose it."         - Robin Williams

Re: help on dereferencing structure
by Anonymous Monk on Sep 11, 2014 at 02:15 UTC