home | O'Reilly's CD bookshelfs | FreeBSD | Linux | Cisco | Cisco Exam  

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:




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).


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


A two-byte integer value.


A four-byte integer value.


An IEEE four-byte real value.


An IEEE eight-byte real value.


An automation currency value.


An automation date value.


A string value.


The value contains another automation object.


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.


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.


The value contains another Variant.


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


An unsigned one-byte character.


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


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

Library Navigation Links

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