vSphere SDK for Perl scripts retrieve objects, such as virtual machines, from the server and work with these objects. vSphere SDK for Perl scripts follow the basic pattern shown in Basic vSphere SDK for Perl Script (simpleclient.pl).
Important The sample script does not use filters or property filters for efficiency. See Refining vSphere SDK for Perl Scripts for information about those topics.
Basic vSphere SDK for Perl Script (simpleclient.pl) All vSphere SDK for Perl scripts must use the VMware::VIRuntime module:
■ The Opts package subroutines handle built-in options and creating custom options.
■ The Util package subroutines facilitate routine tasks, such as setting up and closing connections to the server.
■ The Vim package subroutines access server-side managed objects, instantiate local proxy objects (views), update properties, and run local methods that result in operations on remote servers.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.A number of common command-line options, most of them connection options, are already defined for all utility applications (see Options Available for All SDK for Perl Commands). In addition, most applications have application-specific options you pass to the script at execution time.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.
1 The example declares the option as a hash. The hash key is the option name, and the value is a hashref containing Getopt::Long-style option attributes. See Attributes for Defining New Options for attribute details.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.
Default value used if the option is not explicitly set. An unset option with no default returns undef to the calling script. Enables creating derived options. Set func to an external code reference to run the code when the SDK queries the value of the option. Uses Perl Getopt-style syntax to specify option type and whether the option is required or optional.
2 The example adds the option to the default options using the Opts::add_options() subroutine: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().
Note Your script must call Opts::parse() and Opts::validate() to process the options available for all scripts, even if you do not define script-specific command-line options.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 SDK for Perl Commands.A call to Util::connect() connects to the server.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.Example: Sample Script (Commented Version)
2 Change to the directory that contains the simpleclient.pl script.