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 />
Omitting Optional Arguments in Method Calls<br />
When you call a <strong>vSphere</strong> API method using <strong>vSphere</strong> <strong>SDK</strong> <strong>for</strong> <strong>Perl</strong>, and want to omit an optional argument,<br />
you can do one of two things:<br />
You can omit the argument:<br />
$vm->PowerOnVM(host => $host); # with the optional host argument<br />
$vm->PowerOnVM(); # without the optional host argument<br />
You can supply undef as the value of the optional argument:<br />
$vm->PowerOnVM(host => undef);<br />
Supplying undef as the value of the optional argument is useful when the value of an argument, which might<br />
or might not be undef, is contained in a variable, as in the following example:<br />
my $host = Vim::find_entity_view(<br />
view_type => 'HostSystem',<br />
filter => { name => 'preferredHost' }<br />
);<br />
$vm->PowerOnVM(host => $host);<br />
You cannot use the empty string or the value 0 to represent undef or an unset parameter.<br />
Updating View Objects<br />
In any view, the values of the view properties represent the state of the server‐side objects at the time the view<br />
was created. These property values are not updated automatically. In a production environment, the state of<br />
managed objects on the server is likely to change frequently. If your client script depends on the server being<br />
in a particular state (poweredOn or poweredOff, <strong>for</strong> example), then you can refresh the view object’s state. You<br />
can use the <strong>vSphere</strong> <strong>SDK</strong> <strong>for</strong> <strong>Perl</strong> Vim::update_view_data() subroutine to refresh the values of client‐side<br />
views with server‐side values. Example 2‐3 uses Vim::update_view_data() to refresh view data.<br />
Example 2-3. Updating the State of View Objects<br />
#!/usr/bin/perl<br />
use strict;<br />
use warnings;<br />
use <strong>VMware</strong>::VIRuntime;<br />
. . .<br />
# Get all VirtualMachine objects<br />
my $vm_views = Vim::find_entity_views(view_type => 'VirtualMachine');<br />
# Power off virtual machines.<br />
<strong>for</strong>each my $vm (@$vm_views) {<br />
# Refresh the state of each view<br />
$vm->update_view_data();<br />
if ($vm->runtime->powerState->val eq 'poweredOn') {<br />
$vm->PowerOffVM();<br />
print " Stopped virtual machine: " . $vm->name . "\n";<br />
} else {<br />
print " Virtual machine " . $vm->name .<br />
" power state is: " . $vm->runtime->powerState->val . "\n";<br />
}<br />
}<br />
32 <strong>VMware</strong>, Inc.