📄 ch23_04.htm
字号:
<html><head><title>Win32::OLE::Variant (Perl in a Nutshell, 2nd Edition)</title><link rel="stylesheet" type="text/css" href="../style/style1.css" /><meta name="DC.Creator" content="Stephen Spainhour" /><meta name="DC.Format" content="text/xml" scheme="MIME" /><meta name="DC.Language" content="en-US" /><meta name="DC.Publisher" content="O'Reilly & Associates, Inc." /><meta name="DC.Source" scheme="ISBN" content="0596002416L" /><meta name="DC.Subject.Keyword" content="stuff" /><meta name="DC.Title" content="Perl in a Nutshell, 2nd Edition" /><meta name="DC.Type" content="Text.Monograph" /></head><body bgcolor="#ffffff"><img src="gifs/smbanner.gif" usemap="#banner-map" border="0" alt="Book Home" /><map name="banner-map"><area shape="rect" coords="1,-2,616,66" href="index.htm" alt="Java and XSLT" /><area shape="rect" coords="629,-11,726,25" href="jobjects/fsearch.htm" alt="Search this book" /></map><div class="navbar"><table width="684" border="0"><tr><td align="left" valign="top" width="228"><a href="ch23_03.htm"><img src="../gifs/txtpreva.gif" alt="Previous" border="0" /></a></td><td align="center" valign="top" width="228" /><td align="right" valign="top" width="228"><a href="ch23_05.htm"><img src="../gifs/txtnexta.gif" alt="Next" border="0" /></a></td></tr></table></div><h2 class="sect1">23.4. Win32::OLE::Variant</h2><p><a name="INDEX-3461" /><a name="INDEX-3462" />All automation data has to be coercedinto a special type called a Variant. Most of the time, youdon't need to worry about explicit type coercion.You just provide your scalar data, and the magic of automation takescare of the rest. However, there are times when you want to controlthe exact type of data you're sending to theautomation server. The Win32::OLE::Variant module provides access tothe Variant data type and lets you control exactly how the data isrepresented.</p><p>A Variant is an OLE data structure that contains a type field and adata field. The flags are implemented in Perl (as are many constants)as subroutines that return an integer value. The following tablelists the Variant type flags, along with a brief description of each:</p><a name="ch23-17-fm2xml" /><table border="1" cellpadding="3"><tr><th><p>Type</p></th><th><p>Description</p></th></tr><tr><td><p><tt class="literal">VT_EMPTY</tt></p></td><td><p>No value specified. Incidentally, automation does not use<tt class="literal">VT_EMPTY</tt> for empty optional parameters. Rather, ituses VT_ERROR with a value of <tt class="literal">DISP_E_PARAMNOTFOUND</tt>(which isn't exported by Perl: the value in currentWin32 SDK headers is <tt class="literal">0x80020004</tt>).</p></td></tr><tr><td><p><tt class="literal">VT_NULL</tt></p></td><td><p>A propagating <tt class="literal">NULL</tt> value was specified (not to beconfused with a null pointer). This is used for things like the NULLin SQL.</p></td></tr><tr><td><p><tt class="literal">VT_I2</tt></p></td><td><p>A two-byte integer value.</p></td></tr><tr><td><p><tt class="literal">VT_I4</tt></p></td><td><p>A four-byte integer value.</p></td></tr><tr><td><p><tt class="literal">VT_R4</tt></p></td><td><p>An IEEE four-byte real value.</p></td></tr><tr><td><p><tt class="literal">VT_R8</tt></p></td><td><p>An IEEE eight-byte real value.</p></td></tr><tr><td><p><tt class="literal">VT_CY</tt></p></td><td><p>An automation currency value.</p></td></tr><tr><td><p><tt class="literal">VT_DATE</tt></p></td><td><p>An automation date value.</p></td></tr><tr><td><p><tt class="literal">VT_BSTR</tt></p></td><td><p>A string value.</p></td></tr><tr><td><p><tt class="literal">VT_DISPATCH</tt></p></td><td><p>The value contains another automation object.</p></td></tr><tr><td><p><tt class="literal">VT_ERROR</tt></p></td><td><p>An error code was specified. The type of the error is determined bythe actual value. As mentioned earlier, this is used to implementempty optional parameters.</p></td></tr><tr><td><p><tt class="literal">VT_BOOL</tt></p></td><td><p>A Boolean (true/false) value. If all bits are <tt class="literal">1</tt>,it's true; if all bits are <tt class="literal">0</tt>,it's false. Any other value is invalid.</p></td></tr><tr><td><p><tt class="literal">VT_VARIANT</tt></p></td><td><p>The value contains another Variant.</p></td></tr><tr><td><p><tt class="literal">VT_UNKNOWN</tt></p></td><td><p>The value contains an IUnknown pointer (the base class of COMobjects).</p></td></tr><tr><td><p><tt class="literal">VT_UI1</tt></p></td><td><p>An unsigned one-byte character.</p></td></tr><tr><td><p><tt class="literal">VT_BYREF</tt></p></td><td><p>Can be combined with some fields to indicate that the data is beingpassed by reference, rather than by value.</p></td></tr><tr><td><p><tt class="literal">VT_ARRAY</tt></p></td><td><p>The value contains an OLE SAFEARRAY (this flag is not currentlyexported by Perl).</p></td></tr></table><p><p>To convert data to a specific Variant type, create a variant objectwith either the <tt class="literal">new</tt> constructor method or theconvenience function <tt class="literal">Variant</tt><a name="INDEX-3463" />:</p><blockquote><pre class="code">$vnt = Win32::OLE::Variant->new(type, data);$vnt = Variant(type, data);</pre></blockquote><p>For example, to force a string to be interpreted as a date, create aVariant object and set it to the <tt class="literal">VT_DATE</tt> type:</p><blockquote><pre class="code">$dt = Variant(VT_DATE, "August 24, 1970"); # Create an explicit data type$sheet->Cells(1,1)->{Value} = $dt; # Set it to a spreadsheet cell</pre></blockquote><a name="perlnut2-CHP-23-SECT-4.1" /><div class="sect2"><h3 class="sect2">23.4.1. Win32::OLE::Variant Methods</h3><p><a name="INDEX-3464" />Thefollowing methods are defined by Win32::OLE::Variant for working withVariant data types.</p><a name="INDEX-3465" /><div class="refentry"><table width="515" border="0" cellpadding="5"><tr><td align="left"><font size="+1"><b>As</b></font></td><td align="right"><i></i></td></tr></table><hr width="515" size="3" noshade="true" align="left" color="black" /><pre>$<em class="replaceable">vnt</em>->As(<em class="replaceable">type</em>)</pre><p><a name="INDEX-3465" />Takes a<em class="replaceable"><tt>type</tt></em> flag argument and converts theVariant object to the supplied type before converting it to a Perlvalue.</p></div><a name="INDEX-3466" /><div class="refentry"><table width="515" border="0" cellpadding="5"><tr><td align="left"><font size="+1"><b>ChangeType</b></font></td><td align="right"><i></i></td></tr></table><hr width="515" size="3" noshade="true" align="left" color="black" /><pre>$<em class="replaceable">vnt</em>->ChangeType(<em class="replaceable">type</em>)</pre><p><a name="INDEX-3466" />Takes a<em class="replaceable"><tt>type</tt></em> flag argument and converts theVariant object (in place) to the supplied type.</p></div><a name="INDEX-3467" /><div class="refentry"><table width="515" border="0" cellpadding="5"><tr><td align="left"><font size="+1"><b>Type</b></font></td><td align="right"><i></i></td></tr></table><hr width="515" size="3" noshade="true" align="left" color="black" /><pre>$<em class="replaceable">vnt</em>->Type( )</pre><p><a name="INDEX-3467" />Returns the type ofthe variant <tt class="literal">$</tt><em class="replaceable"><tt>vnt</tt></em>.</p></div><a name="INDEX-3468" /><div class="refentry"><table width="515" border="0" cellpadding="5"><tr><td align="left"><font size="+1"><b>Value</b></font></td><td align="right"><i></i></td></tr></table><hr width="515" size="3" noshade="true" align="left" color="black" /><pre>$<em class="replaceable">vnt</em>->Value( )</pre><p><a name="INDEX-3468" />Returns the value ofthe variant <tt class="literal">$</tt><em class="replaceable"><tt>vnt</tt></em> as aPerl value. The conversion is performed in the same manner as allreturn values of Win32::OLE method calls are converted.</p></div></div><hr width="684" align="left" /><div class="navbar"><table width="684" border="0"><tr><td align="left" valign="top" width="228"><a href="ch23_03.htm"><img src="../gifs/txtpreva.gif" alt="Previous" border="0" /></a></td><td align="center" valign="top" width="228"><a href="index.htm"><img src="../gifs/txthome.gif" alt="Home" border="0" /></a></td><td align="right" valign="top" width="228"><a href="ch23_05.htm"><img src="../gifs/txtnexta.gif" alt="Next" border="0" /></a></td></tr><tr><td align="left" valign="top" width="228">23.3. Win32::OLE::Enum</td><td align="center" valign="top" width="228"><a href="index/index.htm"><img src="../gifs/index.gif" alt="Book Index" border="0" /></a></td><td align="right" valign="top" width="228">23.5. Win32::OLE::Const</td></tr></table></div><hr width="684" align="left" /><img src="../gifs/navbar.gif" usemap="#library-map" border="0" alt="Library Navigation Links" /><p><p><font size="-1"><a href="copyrght.htm">Copyright © 2002</a> O'Reilly & Associates. All rights reserved.</font></p><map name="library-map"><area shape="rect" coords="1,0,85,94" href="../index.htm"><area shape="rect" coords="86,1,178,103" href="../lwp/index.htm"><area shape="rect" coords="180,0,265,103" href="../lperl/index.htm"><area shape="rect" coords="267,0,353,105" href="../perlnut/index.htm"><area shape="rect" coords="354,1,446,115" href="../prog/index.htm"><area shape="rect" coords="448,0,526,132" href="../tk/index.htm"><area shape="rect" coords="528,1,615,119" href="../cookbook/index.htm"><area shape="rect" coords="617,0,690,135" href="../pxml/index.htm"></map></body></html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -