Leaked source code of windows server 2003
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

556 lines
18 KiB

  1. # The documentation is at the __END__
  2. package Win32::OLE::Variant;
  3. require Win32::OLE; # Make sure the XS bootstrap has been called
  4. use strict;
  5. use vars qw(@ISA @EXPORT @EXPORT_OK);
  6. use Exporter;
  7. @ISA = qw(Exporter);
  8. @EXPORT = qw(
  9. Variant
  10. VT_EMPTY VT_NULL VT_I2 VT_I4 VT_R4 VT_R8 VT_CY VT_DATE VT_BSTR
  11. VT_DISPATCH VT_ERROR VT_BOOL VT_VARIANT VT_UNKNOWN VT_DECIMAL VT_UI1
  12. VT_ARRAY VT_BYREF
  13. );
  14. @EXPORT_OK = qw(CP_ACP CP_OEMCP nothing);
  15. # Automation data types.
  16. sub VT_EMPTY {0;}
  17. sub VT_NULL {1;}
  18. sub VT_I2 {2;}
  19. sub VT_I4 {3;}
  20. sub VT_R4 {4;}
  21. sub VT_R8 {5;}
  22. sub VT_CY {6;}
  23. sub VT_DATE {7;}
  24. sub VT_BSTR {8;}
  25. sub VT_DISPATCH {9;}
  26. sub VT_ERROR {10;}
  27. sub VT_BOOL {11;}
  28. sub VT_VARIANT {12;}
  29. sub VT_UNKNOWN {13;}
  30. sub VT_DECIMAL {14;} # Officially not allowed in VARIANTARGs
  31. sub VT_UI1 {17;}
  32. sub VT_ARRAY {0x2000;}
  33. sub VT_BYREF {0x4000;}
  34. # For backward compatibility
  35. sub CP_ACP {0;} # ANSI codepage
  36. sub CP_OEMCP {1;} # OEM codepage
  37. use overload
  38. # '+' => 'Add', '-' => 'Sub', '*' => 'Mul', '/' => 'Div',
  39. '""' => sub {$_[0]->As(VT_BSTR)},
  40. '0+' => sub {$_[0]->As(VT_R8)},
  41. fallback => 1;
  42. sub Variant {
  43. return Win32::OLE::Variant->new(@_);
  44. }
  45. sub nothing {
  46. return Win32::OLE::Variant->new(VT_DISPATCH);
  47. }
  48. 1;
  49. __END__
  50. =head1 NAME
  51. Win32::OLE::Variant - Create and modify OLE VARIANT variables
  52. =head1 SYNOPSIS
  53. use Win32::OLE::Variant;
  54. my $var = Variant(VT_DATE, 'Jan 1,1970');
  55. $OleObject->{value} = $var;
  56. $OleObject->Method($var);
  57. =head1 DESCRIPTION
  58. The IDispatch interface used by the Perl OLE module uses a universal
  59. argument type called VARIANT. This is basically an object containing
  60. a data type and the actual data value. The data type is specified by
  61. the VT_xxx constants.
  62. =head2 Functions
  63. =over 8
  64. =item nothing()
  65. The nothing() function returns an empty VT_DISPATCH variant. It can be
  66. used to clear an object reference stored in a property
  67. use Win32::OLE::Variant qw(:DEFAULT nothing);
  68. # ...
  69. $object->{Property} = nothing;
  70. This has the same effect as the Visual Basic statement
  71. Set object.Property = Nothing
  72. The nothing() function is B<not> exported by default.
  73. =item Variant(TYPE, DATA)
  74. This is just a function alias of the C<Win32::OLE::Variant->new()>
  75. method (see below). This function is exported by default.
  76. =back
  77. =head2 Methods
  78. =over 8
  79. =item new(TYPE, DATA)
  80. This method returns a Win32::OLE::Variant object of the specified
  81. TYPE that contains the given DATA. The Win32::OLE::Variant object
  82. can be used to specify data types other than IV, NV or PV (which are
  83. supported transparently). See L<Variants> below for details.
  84. For VT_EMPTY and VT_NULL variants, the DATA argument may be omitted.
  85. For all non-VT_ARRAY variants DATA specifies the initial value.
  86. To create a SAFEARRAY variant, you have to specify the VT_ARRAY flag in
  87. addition to the variant base type of the array elemnts. In this cases
  88. DATA must be a list specifying the dimensions of the array. Each element
  89. can be either an element count (indices 0 to count-1) or an array
  90. reference pointing to the lower and upper array bounds of this dimension:
  91. my $Array = Win32::OLE::Variant->new(VT_ARRAY|VT_R8, [1,2], 2);
  92. This creates a 2-dimensional SAFEARRAY of doubles with 4 elements:
  93. (1,0), (1,1), (2,0) and (2,1).
  94. A special case is the the creation of one-dimensional VT_UI1 arrays with
  95. a string DATA argument:
  96. my $String = Variant(VT_ARRAY|VT_UI1, "String");
  97. This creates a 6 element character array initialized to "String". For
  98. backward compatibility VT_UI1 with a string initializer automatically
  99. implies VT_ARRAY. The next line is equivalent to the previous example:
  100. my $String = Variant(VT_UI1, "String");
  101. If you really need a single character VT_UI1 variant, you have to create
  102. it using a numeric intializer:
  103. my $Char = Variant(VT_UI1, ord('A'));
  104. =item As(TYPE)
  105. C<As> converts the VARIANT to the new type before converting to a
  106. Perl value. This take the current LCID setting into account. For
  107. example a string might contain a ',' as the decimal point character.
  108. Using C<$variant->As(VT_R8)> will correctly return the floating
  109. point value.
  110. The underlying variant object is NOT changed by this method.
  111. =item ChangeType(TYPE)
  112. This method changes the type of the contained VARIANT in place. It
  113. returns the object itself, not the converted value.
  114. =item Copy([DIM])
  115. This method creates a copy of the object. If the original variant had
  116. the VT_BYREF bit set then the new object will contain a copy of the
  117. referenced data and not a reference to the same old data. The new
  118. object will not have the VT_BYREF bit set.
  119. my $Var = Variant(VT_I4|VT_ARRAY|VT_BYREF, [1,5], 3);
  120. my $Copy = $Var->Copy;
  121. The type of C<$Copy> is now VT_I4|VT_ARRAY and the value is a copy of
  122. the other SAFEARRAY. Changes to elements of C<$Var> will not be reflected
  123. in C<$Copy> and vice versa.
  124. The C<Copy> method can also be used to extract a single element of a
  125. VT_ARRAY | VT_VARIANT object. In this case the array indices must be
  126. specified as a list DIM:
  127. my $Int = $Var->Copy(1, 2);
  128. C<$Int> is now a VT_I4 Variant object containing the value of element (1,2).
  129. =item Currency([FORMAT[, LCID]])
  130. This method converts the VARIANT value into a formatted curency string. The
  131. FORMAT can be either an integer constant or a hash reference. Valid constants
  132. are 0 and LOCALE_NOUSEROVERRIDE. You get the value of LOCALE_NOUSEROVERRIDE
  133. from the Win32::OLE::NLS module:
  134. use Win32::OLE::NLS qw(:LOCALE);
  135. LOCALE_NOUSEROVERRIDE tells the method to use the system default currency
  136. format for the specified locale, disregarding any changes that might have
  137. been made through the control panel application.
  138. The hash reference could contain the following keys:
  139. NumDigits number of fractional digits
  140. LeadingZero whether to use leading zeroes in decimal fields
  141. Grouping size of each group of digits to the left of the decimal
  142. DecimalSep decimal separator string
  143. ThousandSep thousand separator string
  144. NegativeOrder see L<Win32::OLE::NLS/LOCALE_ICURRENCY>
  145. PositiveOrder see L<Win32::OLE::NLS/LOCALE_INEGCURR>
  146. CurrencySymbol currency symbol string
  147. For example:
  148. use Win32::OLE::Variant;
  149. use Win32::OLE::NLS qw(:DEFAULT :LANG :SUBLANG :DATE :TIME);
  150. my $lcidGerman = MAKELCID(MAKELANGID(LANG_GERMAN, SUBLANG_NEUTRAL));
  151. my $v = Variant(VT_CY, "-922337203685477.5808");
  152. print $v->Currency({CurrencySymbol => "Tuits"}, $lcidGerman), "\n";
  153. will print:
  154. -922.337.203.685.477,58 Tuits
  155. =item Date([FORMAT[, LCID]])
  156. Converts the VARIANT into a formatted date string. FORMAT can be either
  157. one of the following integer constants or a format string:
  158. LOCALE_NOUSEROVERRIDE system default date format for this locale
  159. DATE_SHORTDATE use the short date format (default)
  160. DATE_LONGDATE use the long date format
  161. DATE_YEARMONTH use the year/month format
  162. DATE_USE_ALT_CALENDAR use the alternate calendar, if one exists
  163. DATE_LTRREADING left-to-right reading order layout
  164. DATE_RTLREADING right-to left reading order layout
  165. The constants are available from the Win32::OLE::NLS module:
  166. use Win32::OLE::NLS qw(:LOCALE :DATE);
  167. The following elements can be used to construct a date format string.
  168. Characters must be specified exactly as given below (e.g. "dd" B<not> "DD").
  169. Spaces can be inserted anywhere between formating codes, other verbatim
  170. text should be included in single quotes.
  171. d day of month
  172. dd day of month with leading zero for single-digit days
  173. ddd day of week: three-letter abbreviation (LOCALE_SABBREVDAYNAME)
  174. dddd day of week: full name (LOCALE_SDAYNAME)
  175. M month
  176. MM month with leading zero for single-digit months
  177. MMM month: three-letter abbreviation (LOCALE_SABBREVMONTHNAME)
  178. MMMM month: full name (LOCALE_SMONTHNAME)
  179. y year as last two digits
  180. yy year as last two digits with leading zero for years less than 10
  181. yyyy year represented by full four digits
  182. gg period/era string
  183. For example:
  184. my $v = Variant(VT_DATE, "April 1 99");
  185. print $v->Date(DATE_LONGDATE), "\n";
  186. print $v->Date("ddd',' MMM dd yy"), "\n";
  187. will print:
  188. Thursday, April 01, 1999
  189. Thu, Apr 01 99
  190. =item Dim()
  191. Returns a list of array bounds for a VT_ARRAY variant. The list contains
  192. an array reference for each dimension of the variant's SAFEARRAY. This
  193. reference points to an array containing the lower and upper bounds for
  194. this dimension. For example:
  195. my @Dim = $Var->Dim;
  196. Now C<@Dim> contains the following list: C<([1,5], [0,2])>.
  197. =item Get(DIM)
  198. For normal variants C<Get> returns the value of the variant, just like the
  199. C<Value> method. For VT_ARRAY variants C<Get> retrieves the value of a single
  200. array element. In this case C<DIM> must be a list of array indices. E.g.
  201. my $Val = $Var->Get(2,0);
  202. As a special case for one dimensional VT_UI1|VT_ARRAY variants the C<Get>
  203. method without arguments returns the character array as a Perl string.
  204. print $String->Get, "\n";
  205. =item LastError()
  206. The use of the C<Win32::OLE::Variant->LastError()> method is deprecated.
  207. Please use the C<Win32::OLE->LastError()> class method instead.
  208. =item Number([FORMAT[, LCID]])
  209. This method converts the VARIANT value into a formatted number string. The
  210. FORMAT can be either an integer constant or a hash reference. Valid constants
  211. are 0 and LOCALE_NOUSEROVERRIDE. You get the value of LOCALE_NOUSEROVERRIDE
  212. from the Win32::OLE::NLS module:
  213. use Win32::OLE::NLS qw(:LOCALE);
  214. LOCALE_NOUSEROVERRIDE tells the method to use the system default number
  215. format for the specified locale, disregarding any changes that might have
  216. been made through the control panel application.
  217. The hash reference could contain the following keys:
  218. NumDigits number of fractional digits
  219. LeadingZero whether to use leading zeroes in decimal fields
  220. Grouping size of each group of digits to the left of the decimal
  221. DecimalSep decimal separator string
  222. ThousandSep thousand separator string
  223. NegativeOrder see L<Win32::OLE::NLS/LOCALE_INEGNUMBER>
  224. =item Put(DIM, VALUE)
  225. The C<Put> method is used to assign a new value to a variant. The value will
  226. be coerced into the current type of the variant. E.g.:
  227. my $Var = Variant(VT_I4, 42);
  228. $Var->Put(3.1415);
  229. This changes the value of the variant to C<3> because the type is VT_I4.
  230. For VT_ARRAY type variants the indices for each dimension of the contained
  231. SAFEARRAY must be specified in front of the new value:
  232. $Array->Put(1, 1, 2.7);
  233. It is also possible to assign values to *every* element of the SAFEARRAY at
  234. once using a single Put() method call:
  235. $Array->Put([[1,2], [3,4]]);
  236. In this case the argument to Put() must be an array reference and the
  237. dimensions of the Perl list-of-lists must match the dimensions of the
  238. SAFEARRAY exactly.
  239. The are a few special cases for one-dimensional VT_UI1 arrays: The VALUE
  240. can be specified as a string instead of a number. This will set the selected
  241. character to the first character of the string or to '\0' if the string was
  242. empty:
  243. my $String = Variant(VT_UI1|VT_ARRAY, "ABCDE");
  244. $String->Put(1, "123");
  245. $String->Put(3, ord('Z'));
  246. $String->Put(4, '');
  247. This will set the value of C<$String> to C<"A1CZ\0">. If the index is omitted
  248. then the string is copied to the value completely. The string is truncated
  249. if it is longer than the size of the VT_UI1 array. The result will be padded
  250. with '\0's if the string is shorter:
  251. $String->Put("String");
  252. Now C<$String> contains the value "Strin".
  253. C<Put> returns the Variant object itself so that multiple C<Put> calls can be
  254. chained together:
  255. $Array->Put(0,0,$First_value)->Put(0,1,$Another_value);
  256. =item Time([FORMAT[, LCID]])
  257. Converts the VARIANT into a formatted time string. FORMAT can be either
  258. one of the following integer constants or a format string:
  259. LOCALE_NOUSEROVERRIDE system default time format for this locale
  260. TIME_NOMINUTESORSECONDS don't use minutes or seconds
  261. TIME_NOSECONDS don't use seconds
  262. TIME_NOTIMEMARKER don't use a time marker
  263. TIME_FORCE24HOURFORMAT always use a 24-hour time format
  264. The constants are available from the Win32::OLE::NLS module:
  265. use Win32::OLE::NLS qw(:LOCALE :TIME);
  266. The following elements can be used to construct a time format string.
  267. Characters must be specified exactly as given below (e.g. "dd" B<not> "DD").
  268. Spaces can be inserted anywhere between formating codes, other verbatim
  269. text should be included in single quotes.
  270. h hours; 12-hour clock
  271. hh hours with leading zero for single-digit hours; 12-hour clock
  272. H hours; 24-hour clock
  273. HH hours with leading zero for single-digit hours; 24-hour clock
  274. m minutes
  275. mm minutes with leading zero for single-digit minutes
  276. s seconds
  277. ss seconds with leading zero for single-digit seconds
  278. t one character time marker string, such as A or P
  279. tt multicharacter time marker string, such as AM or PM
  280. For example:
  281. my $v = Variant(VT_DATE, "April 1 99 2:23 pm");
  282. print $v->Time, "\n";
  283. print $v->Time(TIME_FORCE24HOURFORMAT|TIME_NOTIMEMARKER), "\n";
  284. print $v->Time("hh.mm.ss tt"), "\n";
  285. will print:
  286. 2:23:00 PM
  287. 14:23:00
  288. 02.23.00 PM
  289. =item Type()
  290. The C<Type> method returns the variant type of the contained VARIANT.
  291. =item Unicode()
  292. The C<Unicode> method returns a C<Unicode::String> object. This contains
  293. the BSTR value of the variant in network byte order. If the variant is
  294. not currently in VT_BSTR format then a VT_BSTR copy will be produced first.
  295. =item Value()
  296. The C<Value> method returns the value of the VARIANT as a Perl value. The
  297. conversion is performed in the same manner as all return values of
  298. Win32::OLE method calls are converted.
  299. =back
  300. =head2 Overloading
  301. The Win32::OLE::Variant package has overloaded the conversion to
  302. string and number formats. Therefore variant objects can be used in
  303. arithmetic and string operations without applying the C<Value>
  304. method first.
  305. =head2 Class Variables
  306. The Win32::OLE::Variant class used to have its own set of class variables
  307. like C<$CP>, C<$LCID> and C<$Warn>. In version 0.1003 and later of the
  308. Win32::OLE module these variables have been eleminated. Now the settings
  309. of Win32::OLE are used by the Win32::OLE::Variant module too. Please read
  310. the documentation of the C<Win32::OLE-&gt;Option> class method.
  311. =head2 Constants
  312. These constants are exported by default:
  313. VT_EMPTY
  314. VT_NULL
  315. VT_I2
  316. VT_I4
  317. VT_R4
  318. VT_R8
  319. VT_CY
  320. VT_DATE
  321. VT_BSTR
  322. VT_DISPATCH
  323. VT_ERROR
  324. VT_BOOL
  325. VT_VARIANT
  326. VT_UNKNOWN
  327. VT_DECIMAL
  328. VT_UI1
  329. VT_ARRAY
  330. VT_BYREF
  331. VT_DECIMAL is not on the official list of allowable OLE Automation
  332. datatypes. But even Microsoft ADO seems to sometimes return values
  333. of Recordset fields in VT_DECIMAL format.
  334. =head2 Variants
  335. A Variant is a data type that is used to pass data between OLE
  336. connections.
  337. The default behavior is to convert each perl scalar variable into
  338. an OLE Variant according to the internal perl representation.
  339. The following type correspondence holds:
  340. C type Perl type OLE type
  341. ------ --------- --------
  342. int IV VT_I4
  343. double NV VT_R8
  344. char * PV VT_BSTR
  345. void * ref to AV VT_ARRAY
  346. ? undef VT_ERROR
  347. ? Win32::OLE object VT_DISPATCH
  348. Note that VT_BSTR is a wide character or Unicode string. This presents a
  349. problem if you want to pass in binary data as a parameter as 0x00 is
  350. inserted between all the bytes in your data. The C<Variant()> method
  351. provides a solution to this. With Variants the script writer can specify
  352. the OLE variant type that the parameter should be converted to. Currently
  353. supported types are:
  354. VT_UI1 unsigned char
  355. VT_I2 signed int (2 bytes)
  356. VT_I4 signed int (4 bytes)
  357. VT_R4 float (4 bytes)
  358. VT_R8 float (8 bytes)
  359. VT_DATE OLE Date
  360. VT_BSTR OLE String
  361. VT_CY OLE Currency
  362. VT_BOOL OLE Boolean
  363. When VT_DATE and VT_CY objects are created, the input parameter is treated
  364. as a Perl string type, which is then converted to VT_BSTR, and finally to
  365. VT_DATE of VT_CY using the C<VariantChangeType()> OLE API function.
  366. See L<Win32::OLE/EXAMPLES> for how these types can be used.
  367. =head2 Variant arrays
  368. A variant can not only contain a single value but also a multi-dimensional
  369. array of values (called a SAFEARRAY). In this case the VT_ARRAY flag must
  370. be added to the base variant type, e.g. C<VT_I4 | VT_ARRAY> for an array of
  371. integers. The VT_EMPTY and VT_NULL types are invalid for SAFEARRAYs. It
  372. is possible to create an array of variants: C<VT_VARIANT | VT_ARRAY>. In this
  373. case each element of the array can have a different type (including VT_EMPTY
  374. and VT_NULL). The elements of a VT_VARIANT SAFEARRAY cannot have either of the
  375. VT_ARRAY or VT_BYREF flags set.
  376. The lower and upper bounds for each dimension can be specified separately.
  377. They do not have to have all the same lower bound (unlike Perl's arrays).
  378. =head2 Variants by reference
  379. Some OLE servers expect parameters passed by reference so that they
  380. can be changed in the method call. This allows methods to easily
  381. return multiple values. There is preliminary support for this in
  382. the Win32::OLE::Variant module:
  383. my $x = Variant(VT_I4|VT_BYREF, 0);
  384. my $y = Variant(VT_I4|VT_BYREF, 0);
  385. $Corel->GetSize($x, $y);
  386. print "Size is $x by $y\n";
  387. After the C<GetSize> method call C<$x> and C<$y> will be set to
  388. the respective sizes. They will still be variants. In the print
  389. statement the overloading converts them to string representation
  390. automatically.
  391. VT_BYREF is now supported for all variant types (including SAFEARRAYs).
  392. It can also be used to pass an OLE object by reference:
  393. my $Results = $App->CreateResultsObject;
  394. $Object->Method(Variant(VT_DISPATCH|VT_BYREF, $Results));
  395. =head1 AUTHORS/COPYRIGHT
  396. This module is part of the Win32::OLE distribution.
  397. =cut