Learning Perl on Win32 Systems

Learning Perl on Win32 SystemsSearch this book
Previous: 19.3 Using Automation ObjectsChapter 19
OLE Automation
Next: 19.5 Tips and Techniques
 

19.4 Variants

In order for automation controllers and servers to cooperate, they have to have some way to agree on the type of data that they're passing. Automation accomplishes this through a data type called a VARIANT. The VARIANT data type is built on a C-language union. It contains a type field that identifies that type of data in the union (things such as strings, numbers, automation objects, etc.) and a field that contains the data.

Usually, Perl handles data-type conversion for you. If you need more control, though, you can create a Variant object and specify the type yourself. Perl provides access to the types listed in Table 19.3.


Table 19.3: Variant Types

Variant Type

Description

VT_UI1

Unsigned character (1 byte)

VT_I2

Signed integer (2 bytes)

VT_I4

Signed integer (4 bytes)

VT_R4

Floating point (4 bytes)

VT_R8

Floating point (8 bytes)

VT_DATE

OLE Date (floating-point value measuring days since midnight, Dec. 30, 1899)

VT_BSTR

OLE String

VT_CY

OLE Currency

VT_BOOL

OLE Boolean

By default, Perl converts integer data to the VT_I4 type, string data to the VT_BSTR type, and floating-point data to the VT_R8 type. Usually, these conversions are what you'd expect, but let's look at how you might specify your own type:

$vt = new OLE::Variant(OLE::VT_DATE, "May 31, 1997" );
$Message->{TimeSent} = $vt;

This example first creates a Variant object, setting the type to VT_DATE and the date to "May 31, 1997." It then assigns the date to the Message object TimeSent property (something you might do if you were posting a message to a folder, for example).


Previous: 19.3 Using Automation ObjectsLearning Perl on Win32 SystemsNext: 19.5 Tips and Techniques
19.3 Using Automation ObjectsBook Index19.5 Tips and Techniques