6.5
Table Of Contents
- vSphere SDK for Perl Programming Guide
- Contents
- Getting Started with vSphere SDK for Perl
- Writing vSphere SDK for Perl Scripts
- Refining vSphere SDK for Perl Scripts
- Creating and Using Filters
- Filtering Views Selectively Using Properties
- Retrieving the ServiceInstance Object on a vSphere Host
- Saving and Using Sessions
- Using Multiple Sessions
- Learning About Object Structure Using Data::Dumper
- Specifying Untyped Arguments in Scheduled Tasks and Callbacks
- Using Advanced Subroutines
- vSphere SDK for Perl Subroutine Reference
- Web Services for Management Perl Library
- Credential Store Perl Library
VMware, Inc. 21
Chapter 2 Writing vSphere SDK for Perl Scripts
ThevSphereSDKforPerlhasdefinedallcommonoptionsusingattributesandsubroutinesspecifiedinthe
VILib::Optspackage.YoucansimilarlyusetheVILib::Optspackagetocreatecustomoptionsforyourown
applicationsandscripts,tosimplifyuseofyourscript,ortoallowuserstospecifyotherinformation.
Example 2‐
1definesanentityoptionthatmustbemadeavailabletothescriptatruntime.Theoption
specifieswhichoftheavailableentitytypesispassedasaparametertotheVim::find_entity_views()
subroutineforfurtherprocessing.AnydirectorindirectsubclassofManagedEntityisavalidoption(for
exampleHostSystem
, ResourcePool,orVirtualMachine).Theexamplecreatesandparsesanew
command‐lineoption.
1Theexampledeclarestheoptionasahash.Thehashkeyistheoptionname,andthevalueisahashref
containingGetopt::Long‐styleoptionattributes.SeeTable 2‐2forattributedetails.
Example 2‐1createsarequiredcommand‐lineoption
thatacceptsastringvalue,asfollows:
my %opts = (
entity => {
type => "=s",
variable => "VI_ENTITY",
help => "ManagedEntity type: HostSystem, etc",
required => 1,
},
);
Table 2‐2listsallattributesyoucanusetodefinecommand‐lineoptions.Thecodefragmentin“Step1:
ImportthevSphereSDKforPerlModules”onpage 20aboveusesonlytype,variable,help,and
required.Forrelatedinformation,seethedocumentationfortheGetopt::Longmodule.
2Theexampleadds
theoptiontothedefaultoptionsusingtheOpts::add_options()subroutine:
Opts::add_options(%opts);
3Theexampleparsesandvalidatestheoptionsbeforeconnectingtotheserver,asfollows:
Opts::parse();
Opts::validate();
InExample 2‐1,theentityoptionisrequired,sothescriptcannotrununlesstheuserpassesintheoption
nameandvalue(see“SpecifyingOptions”onpage 12).
Table 2-2. Attributes for Defining New Options
Attribute Description
default Defaultvalueusediftheoptionisnotexplicitlyset.Anunsetoptionwithnodefaultreturnsundef
tothecallingscript.
func Enablescreatingderivedoptions.Setfunctoanexternalcodereferencetorunthecodewhenthe
SDKqueriesthevalueoftheoption.
help Descriptivetextexplainingtheoption,
displayedinthescript’shelpmessage.
required Ifthisattributeissetto1,usersmustprovideavalueforthisoptionorthescriptexitsanddisplay
thehelpmessage.Setto1torequireavalue.Setto0ifthevalueisoptional.
variable Allowsyoutospecifytheoption
inanenvironmentvariableoraconfigurationfile.See“Specifying
Options”onpage 12.
type UsesPerlGetopt‐stylesyntaxtospecifyoptiontypeandwhethertheoptionisrequiredoroptional.
Usedoublequotestoindicatethatoptiondoesn’tacceptavalue.Thedefaultnumericvalueis0.The
defaultstringvalue
isʺʺ(emptystring).Youcanuseoneofthefollowing:
Equalsign(=)—mandatory
Colon(:)—optional
s—string
i—integer
f—float