home *** CD-ROM | disk | FTP | other *** search
/ Netrunner 2004 October / NETRUNNER0410.ISO / regular / ActivePerl-5.8.4.810-MSWin32-x86.msi / _e58fae5514b04f44f36b9c1739dee4d3 < prev    next >
Encoding:
Text File  |  2004-06-01  |  18.5 KB  |  578 lines

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