This module handles all client-side infrastructure details. For example, it transparently maps data types and provides local Perl interfaces to server-side objects. The module also loads subroutines that you can use to connect to a vCenter Server or ESX/ESXi system and to retrieve views. Views are the client-side Perl objects that encapsulate the properties and operations of server-side managed objects. The subroutines are organized into different packages:
When you run a script from the command line, you usually specify connection information and might also specify other information such as a virtual machine that you want to power off or a host for which you need status information. vSphere SDK for Perl lets you specify these options in a variety of ways. See
Specifying Options.
The vSphere SDK for Perl has defined all common options using attributes and subroutines specified in the VILib::Opts package. You can similarly use the
VILib::Opts package to create custom options for your own applications and scripts, to simplify use of your script, or to allow users to specify other information.
Example: Basic vSphere SDK for Perl Script (simpleclient.pl) defines an
entity option that must be made available to the script at runtime. The option specifies which of the available entity types is passed as a parameter to the
Vim::find_entity_views() subroutine for further processing. Any direct or indirect subclass of
ManagedEntity is a valid option (for example
HostSystem, ResourcePool, or
VirtualMachine). The example creates and parses a new command-line option.
Example: Basic vSphere SDK for Perl Script (simpleclient.pl) creates a required command-line option that accepts a string value, as follows:
Attributes for Defining New Options lists all attributes you can use to define command-line options. The code fragment in
Step 1: Import the vSphere SDK for Perl Modules above uses only
type,
variable,
help, and
required. For related information, see the documentation for the
Getopt::Long module.
In Example: Basic vSphere SDK for Perl Script (simpleclient.pl), the
entity option is required, so the script cannot run unless the user passes in the option name and value (see
Specifying Options).
The Vim::find_entity_views() subroutine uses the value the user passes in later in the script. The value must be one of the managed-entity types listed as
view_type parameter supported by
Vim::find_entity_views().
The vSphere API is hosted as a secure Web service on a vCenter Server and ESX/ESXi system. By default, the Web service is available over HTTPS. Clients must provide valid credentials to connect to the service. Depending on the specifics of your server, you might have to enter only a user name and password. You might need other information. See
Options Available for all vSphere SDK for Perl Scripts.
When a script reaches the call to Util::connect(), the vSphere SDK for Perl runtime checks the environment variables, configuration file contents, and command-line entries (in this order) for connection options. If the options are not defined, the runtime uses the defaults (
localhost and no user name and password) to set up the connection.
When you call the subroutines in the Vim package to retrieve entities from the host, the vSphere SDK for Perl runtime creates the corresponding Perl objects (view objects) locally.
Example: Basic vSphere SDK for Perl Script (simpleclient.pl) uses the
Opts::get_option() subroutine to assign to
$entity_type the string value of the parameter that the user passes in when executing the script.
Example: Basic vSphere SDK for Perl Script (simpleclient.pl) then uses
$entity_type as the
view_type parameter in the subsequent call to
Vim::find_entity_views().
The Vim::find_entity_views() subroutine creates a local Perl object (an array of references) from the server-side managed object of the specified entity type.
The last part of the script processes the views. For this step, you must know the view objects’ properties and methods, so you must understand the server-side objects. See
Understanding Server-Side Objects for an introduction. For in-depth information about server-side objects, see the
vSphere API Reference Guide which is included on the vSphere SDK for Perl documentation page.
Because views are Perl objects, you use Perl object-oriented syntax to process the views. Example: Basic vSphere SDK for Perl Script (simpleclient.pl) loops through the array of entities returned (
@$entity_views) and accesses the
name property of each entity by calling
$entity_view->name. The example then prints the name of each entity to the console.
To log out and exit, use the Util::disconnect() subroutine.
Example: Sample Script (Commented Version) shows the complete listing for
simpleclient.pl.