The vSphere SDK for Perl transparently uses the Data::Dumper
Perl module, which is a standard library, to create the client-side view objects.
Using Data::Dumper to Output Perl Object Structures illustrates how you can use Data::Dumper
to explore view objects and corresponding vSphere objects.
Lines 12 through 14 set several parameters of Data::Dumper
.
Sortkeys
orders the name-value pairs alphabetically by name.Deepcopy
enables deep copying of structures. Deep copying ensures that the output is straightforward and tree-like.Indent
set to 2 causesData::Dumper
to take hash key length into account in the output. The indent results in a more readable format.
Using Data::Dumper to Output Perl Object Structures
01 use strict; 02 use warnings; 03 04 use VMware::VIRuntime; 05 use VMware::VILib; 06 07 # Parse connection options and connect to the server 08 Opts::parse(); 09 Opts::validate(); 10 Util::connect(); 11 12 $Data::Dumper::Sortkeys = 1; #Sort the keys in the output 13 $Data::Dumper::Deepcopy = 1; #Enable deep copies of structures 14 $Data::Dumper::Indent = 2; #Output in a reasonable style (but no array indexes) 15 16 17 18 # Get the view for the target host 19 my $host_view = Vim::find_entity_view(view_type => 'HostSystem'); 20 21 print "The name of this host is ", $host_view->name . "\n\n"; 22 23 print Dumper ($host_view->summary->config->product) . "\n\n\n"; 24 25 print Dumper ($host_view->summary->config) . "\n\n\n"; 26 27 print Dumper ($host_view->summary) . "\n\n\n"; 28 29 # logout 30 Vim::logout();
When you run the entire program, it produces detailed output. The output from line 23 looks as follows.
$VAR1 = bless( { 'apiType' => 'HostAgent', 'apiVersion' => '4.0.0', 'build' => '31178', 'fullName' => 'VMware ESX Server 3.0.1 build-31178', 'localeBuild' => '000', 'localeVersion' => 'INTL', 'name' => 'VMware ESX Server', 'osType' => 'vmnix-x86', 'productLineId' => 'esx', 'vendor' => 'VMware, Inc.', 'version' => '3.0.1' }, 'AboutInfo' );
The output above shows the content of the summary.config.product
property of a HostSystem
managed object. The type (or more properly class) of summary.config.product
property is AboutInfo
. Perl's Data::Dumper
module writes the object in a form that can be used with eval
to get back a copy of the original structure. The bless
keyword indicates the data is a Perl object, and the last argument to bless
is the class of the object, AboutInfo
.
Line 19 (in Using Data::Dumper to Output Perl Object Structures) retrieves the HostSystem view object and line 21 prints the name associated with the corresponding host.
The config
property has more values than those printed by line 23. Line 25 prints the entire config
object. Inside the config
object printed by line 25 (in Using Data::Dumper to Output Perl Object Structures), the product
property is an object. The bless
function returns a reference to the product
object, which is itself nested inside the config
object.
$VAR1 = bless( { 'name' => 'test-system.eng.vmware.com', 'port' => 'nnn', 'product' => bless( { 'apiType' => 'HostAgent', 'apiVersion' => '4.0.0', 'build' => '31178', 'fullName' => 'VMware ESX Server 3.0.1 build-31178', 'localeBuild' => '000', 'localeVersion' => 'INTL', 'name' => 'VMware ESX Server', 'osType' => 'vmnix-x86', 'productLineId' => 'esx', 'vendor' => 'VMware, Inc.', 'version' => '3.0.1' }, 'AboutInfo' ), 'vmotionEnabled' => 'false' }, 'HostConfigSummary' );
The output from line 27 of Using Data::Dumper to Output Perl Object Structures prints the structure of the entire summary
object of the host view. The output shows a number of nested objects, including two objects that are nested two levels deep. The product
object is nested inside the config
object, and the connectionState
object is nested inside the runtime
object.