vSphere SDK for Perl Programming Guide - Documentation - VMware
vSphere SDK for Perl Programming Guide - Documentation - VMware
vSphere SDK for Perl Programming Guide - Documentation - VMware
Create successful ePaper yourself
Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.
<strong>vSphere</strong> <strong>SDK</strong> <strong>for</strong> <strong>Perl</strong> <strong>Programming</strong> <strong>Guide</strong><br />
if (Opts::option_is_set('username2')) {<br />
$username = Opts::get_option('username2');<br />
}<br />
if (Opts::option_is_set('password2')) {<br />
$password = Opts::get_option('password2');<br />
}<br />
$vim_objs[1]->login(user_name => $username, password => $password);<br />
# list VM's <strong>for</strong> all hosts<br />
<strong>for</strong>each my $vim_obj (@vim_objs) {<br />
print "List of virtual machines:\n";<br />
my $vm_views = $vim_obj->find_entity_views(view_type => 'VirtualMachine');<br />
<strong>for</strong>each my $vm (@$vm_views) {<br />
print $vm->name . "\n";<br />
}<br />
print "\n";<br />
}<br />
# logout<br />
<strong>for</strong>each my $vim_obj (@vim_objs) {<br />
$vim_obj->logout();<br />
}<br />
Learning About Object Structure Using Data::Dumper<br />
The <strong>vSphere</strong> <strong>SDK</strong> <strong>for</strong> <strong>Perl</strong> transparently uses the Data::Dumper <strong>Perl</strong> module (a standard library) to create the<br />
client‐side view objects. Example 3‐4 illustrates how you can use Data::Dumper to explore view objects and<br />
corresponding <strong>vSphere</strong> objects.<br />
Lines 12 through 14 set several parameters of Data::Dumper, as follows:<br />
Sortkeys orders the name‐value pairs alphabetically by name.<br />
Deepcopy enables deep copying of structures. Deep copying ensures that the output is straight<strong>for</strong>ward<br />
and tree‐like.<br />
Indent set to 2 causes Data::Dumper to take hash key length into account in the output. The indent<br />
results in a more readable <strong>for</strong>mat.<br />
Example 3-4. Using Data::Dumper to Output <strong>Perl</strong> Object Structures<br />
01 use strict;<br />
02 use warnings;<br />
03<br />
04 use <strong>VMware</strong>::VIRuntime;<br />
05 use <strong>VMware</strong>::VILib;<br />
06<br />
07 # Parse connection options and connect to the server<br />
08 Opts::parse();<br />
09 Opts::validate();<br />
10 Util::connect();<br />
11<br />
12 $Data::Dumper::Sortkeys = 1; #Sort the keys in the output<br />
13 $Data::Dumper::Deepcopy = 1; #Enable deep copies of structures<br />
14 $Data::Dumper::Indent = 2; #Output in a reasonable style (but no array indexes)<br />
15<br />
16<br />
17<br />
18 # Get the view <strong>for</strong> the target host<br />
19 my $host_view = Vim::find_entity_view(view_type => 'HostSystem');<br />
20<br />
21 print "The name of this host is ", $host_view->name . "\n\n";<br />
22<br />
38 <strong>VMware</strong>, Inc.