Book HomeJava and XSLTSearch this book

23.4. Win32::OLE::Variant

All automation data has to be coerced into a special type called a Variant. Most of the time, you don't need to worry about explicit type coercion. You just provide your scalar data, and the magic of automation takes care of the rest. However, there are times when you want to control the exact type of data you're sending to the automation server. The Win32::OLE::Variant module provides access to the Variant data type and lets you control exactly how the data is represented.

A Variant is an OLE data structure that contains a type field and a data field. The flags are implemented in Perl (as are many constants) as subroutines that return an integer value. The following table lists the Variant type flags, along with a brief description of each:

Type

Description

VT_EMPTY

No value specified. Incidentally, automation does not use VT_EMPTY for empty optional parameters. Rather, it uses VT_ERROR with a value of DISP_E_PARAMNOTFOUND (which isn't exported by Perl: the value in current Win32 SDK headers is 0x80020004).

VT_NULL

A propagating NULL value was specified (not to be confused with a null pointer). This is used for things like the NULL in SQL.

VT_I2

A two-byte integer value.

VT_I4

A four-byte integer value.

VT_R4

An IEEE four-byte real value.

VT_R8

An IEEE eight-byte real value.

VT_CY

An automation currency value.

VT_DATE

An automation date value.

VT_BSTR

A string value.

VT_DISPATCH

The value contains another automation object.

VT_ERROR

An error code was specified. The type of the error is determined by the actual value. As mentioned earlier, this is used to implement empty optional parameters.

VT_BOOL

A Boolean (true/false) value. If all bits are 1, it's true; if all bits are 0, it's false. Any other value is invalid.

VT_VARIANT

The value contains another Variant.

VT_UNKNOWN

The value contains an IUnknown pointer (the base class of COM objects).

VT_UI1

An unsigned one-byte character.

VT_BYREF

Can be combined with some fields to indicate that the data is being passed by reference, rather than by value.

VT_ARRAY

The value contains an OLE SAFEARRAY (this flag is not currently exported by Perl).

To convert data to a specific Variant type, create a variant object with either the new constructor method or the convenience function Variant:

$vnt = Win32::OLE::Variant->new(type, data);
$vnt = Variant(type, data);

For example, to force a string to be interpreted as a date, create a Variant object and set it to the VT_DATE type:

$dt = Variant(VT_DATE, "August 24, 1970");   # Create an explicit data type
$sheet->Cells(1,1)->{Value} = $dt;           # Set it to a spreadsheet cell

23.4.1. Win32::OLE::Variant Methods

The following methods are defined by Win32::OLE::Variant for working with Variant data types.

As

$vnt->As(type)

Takes a type flag argument and converts the Variant object to the supplied type before converting it to a Perl value.

ChangeType

$vnt->ChangeType(type)

Takes a type flag argument and converts the Variant object (in place) to the supplied type.

Type

$vnt->Type(  )

Returns the type of the variant $vnt.

Value

$vnt->Value(  )

Returns the value of the variant $vnt as a Perl value. The conversion is performed in the same manner as all return values of Win32::OLE method calls are converted.



Library Navigation Links

Copyright © 2002 O'Reilly & Associates. All rights reserved.