ole.html

来自「perl教程」· HTML 代码 · 共 944 行 · 第 1/5 页

HTML
944 行
字号
<p>Events must be enabled explicitly for an OLE object through the
Win32::OLE-&gt;<a href="#item_withevents"><code>WithEvents()</code></a> class method.  The Win32::OLE module uses the
IProvideClassInfo2 interface to determine the default event source of
the object.  If this interface is not supported, then the user must
specify the name of the event source explicitly in the <a href="#item_withevents"><code>WithEvents()</code></a>
method call.  It is also possible to specify the class name of the
object as the third parameter.  In this case Win32::OLE will try to
look up the default source interface for this COCLASS.</p>
<p>The HANDLER argument to Win32::OLE-&gt;<a href="#item_withevents"><code>WithEvents()</code></a> can either be a CODE
reference or a package name.  In the first case, all events will invoke
this particular function.  The first two arguments to this function will
be the OBJECT itself and the name of the event.  The remaining arguments
will be event specific.</p>
<pre>
        <span class="keyword">sub</span><span class="variable"> Event </span><span class="operator">{</span>
            <span class="keyword">my</span> <span class="operator">(</span><span class="variable">$Obj</span><span class="operator">,</span><span class="variable">$Event</span><span class="operator">,</span><span class="variable">@Args</span><span class="operator">)</span> <span class="operator">=</span> <span class="variable">@_</span><span class="operator">;</span>
            <span class="keyword">print</span> <span class="string">"Event triggered: '$Event'\n"</span><span class="operator">;</span>
        <span class="operator">}</span>
        <span class="variable">Win32::OLE</span><span class="operator">-&gt;</span><span class="variable">WithEvents</span><span class="operator">(</span><span class="variable">$Obj</span><span class="operator">,</span> <span class="operator">\&amp;</span><span class="variable">Event</span><span class="operator">);</span>
</pre>
<p>Alternatively the HANDLER argument can specify a package name.  When the
OBJECT fires an event, Win32::OLE will try to find a function of the same
name as the event in this package.  This function will be called with the
OBJECT as the first argument followed again by the event specific parameters:</p>
<pre>
        <span class="keyword">package</span> <span class="variable">MyEvents</span><span class="operator">;</span>
        <span class="keyword">sub</span><span class="variable"> EventName1 </span><span class="operator">{</span>
            <span class="keyword">my</span> <span class="operator">(</span><span class="variable">$Obj</span><span class="operator">,</span><span class="variable">@Args</span><span class="operator">)</span> <span class="operator">=</span> <span class="variable">@_</span><span class="operator">;</span>
            <span class="keyword">print</span> <span class="string">"EventName1 event triggered\n"</span><span class="operator">;</span>
        <span class="operator">}</span>
</pre>
<pre>
        <span class="keyword">package</span> <span class="variable">main</span><span class="operator">;</span>
        <span class="variable">Win32::OLE</span><span class="operator">-&gt;</span><span class="variable">WithEvents</span><span class="operator">(</span><span class="variable">$Obj</span><span class="operator">,</span> <span class="string">'MyEvents'</span><span class="operator">,</span> <span class="string">'IEventInterface'</span><span class="operator">);</span>
</pre>
<p>If Win32::OLE doesn't find a function with the name of the event then nothing
happens.</p>
<p>Event parameters passed <em>by reference</em> are handled specially.  They are not
converted to the corresponding Perl datatype but passed as Win32::OLE::Variant
objects.  You can assign a new value to these objects with the help of the
<code>Put()</code> method.  This value will be passed back to the object when the event
function returns:</p>
<pre>
        <span class="keyword">package</span> <span class="variable">MyEvents</span><span class="operator">;</span>
        <span class="keyword">sub</span><span class="variable"> BeforeClose </span><span class="operator">{</span>
            <span class="keyword">my</span> <span class="operator">(</span><span class="variable">$self</span><span class="operator">,</span><span class="variable">$Cancel</span><span class="operator">)</span> <span class="operator">=</span> <span class="variable">@_</span><span class="operator">;</span>
            <span class="variable">$Cancel</span><span class="operator">-&gt;</span><span class="variable">Put</span><span class="operator">(</span><span class="number">1</span><span class="operator">)</span> <span class="keyword">unless</span> <span class="variable">$MayClose</span><span class="operator">;</span>
        <span class="operator">}</span>
</pre>
<p>Direct assignment to $Cancel would have no effect on the original value and
would therefore not command the object to abort the closing action.</p>
<p>
</p>
<h2><a name="module_options">Module Options</a></h2>
<p>The following module options can be accessed and modified with the
<code>Win32::OLE-</code>Option&gt; class method.  In earlier versions of the Win32::OLE
module these options were manipulated directly as class variables.  This
practice is now deprecated.</p>
<dl>
<dt><strong><a name="item_cp">CP</a></strong>

<dd>
<p>This variable is used to determine the codepage used by all
translations between Perl strings and Unicode strings used by the OLE
interface.  The default value is CP_ACP, which is the default ANSI
codepage.  Other possible values are CP_OEMCP, CP_MACCP, CP_UTF7 and
CP_UTF8.  These constants are not exported by default.</p>
</dd>
</li>
<dt><strong><a name="item_lcid">LCID</a></strong>

<dd>
<p>This variable controls the locale idnetifier used for all OLE calls.
It is set to LOCALE_NEUTRAL by default.  Please check the
<a href="../../lib/Win32/OLE/NLS.html">the Win32::OLE::NLS manpage</a> module for other locale related information.</p>
</dd>
</li>
<dt><strong><a name="item_variant">Variant</a></strong>

<dd>
<p>This options controls how method calls and property accessors return
values of type VT_CY and VT_DECIMAL are being returned.  By default
VT_CY values are turned into strings and VT_DECIMAL values into
floating point numbers.  If the <a href="#item_variant"><code>Variant</code></a> option is enabled, these
values are returned as Win32::OLE::Variant objects, just like VT_DATE
and VT_ERROR values.  If the Win32::OLE::Variant module is also
loaded, then all values should still behave as before in string and in
numeric context.</p>
</dd>
<dd>
<p>The only reason that the <a href="#item_variant"><code>Variant</code></a> behavior is not the default is that
this is an incompatible change that might break existing programs.</p>
</dd>
</li>
<dt><strong><a name="item_warn">Warn</a></strong>

<dd>
<p>This variable determines the behavior of the Win32::OLE module when
an error happens.  Valid values are:</p>
</dd>
<dd>
<pre>
        0       Ignore error, return undef
        1       Carp::carp if $^W is set (-w option)
        2       always Carp::carp
        3       Carp::croak</pre>
</dd>
<dd>
<p>The error number and message (without Carp line/module info) are
available through the <code>Win32::OLE-</code>LastError&gt; class method.</p>
</dd>
<dd>
<p>Alternatively the Warn option can be set to a CODE reference.  E.g.</p>
</dd>
<dd>
<pre>
    <span class="variable">Win32::OLE</span><span class="operator">-&gt;</span><span class="variable">Option</span><span class="operator">(</span><span class="string">Warn</span> <span class="operator">=&gt;</span> <span class="number">3</span><span class="operator">);</span>
</pre>
</dd>
<dd>
<p>is equivalent to</p>
</dd>
<dd>
<pre>
    <span class="variable">Win32::OLE</span><span class="operator">-&gt;</span><span class="variable">Option</span><span class="operator">(</span><span class="string">Warn</span> <span class="operator">=&gt;</span> <span class="operator">\&amp;</span><span class="variable">Carp::croak</span><span class="operator">);</span>
</pre>
</dd>
<dd>
<p>This can even be used to emulate the VisualBasic <code>On Error Goto
Label</code> construct:</p>
</dd>
<dd>
<pre>
    <span class="variable">Win32::OLE</span><span class="operator">-&gt;</span><span class="variable">Option</span><span class="operator">(</span><span class="string">Warn</span> <span class="operator">=&gt;</span>  <span class="keyword">sub</span><span class="variable"> </span><span class="operator">{</span><span class="keyword">goto</span> <span class="variable">CheckError</span><span class="operator">});</span>
    <span class="comment"># ... your normal OLE code here ...</span>
</pre>
</dd>
<dd>
<pre>
  CheckError:
    # ... your error handling code here ...</pre>
</dd>
</li>
<dt><strong><a name="item__newenum">_NewEnum</a></strong>

<dd>
<p>This option enables additional enumeration support for collection
objects.  When the <a href="#item__newenum"><code>_NewEnum</code></a> option is set, all collections will
receive one additional property: <a href="#item__newenum"><code>_NewEnum</code></a>.  The value of this
property will be a reference to an array containing all the elements
of the collection.  This option can be useful when used in conjunction
with an automatic tree traversal program, like <code>Data::Dumper</code> or an
object tree browser.  The value of this option should be either 1
(enabled) or 0 (disabled, default).</p>
</dd>
<dd>
<pre>
    <span class="variable">Win32::OLE</span><span class="operator">-&gt;</span><span class="variable">Option</span><span class="operator">(</span><span class="string">_NewEnum</span> <span class="operator">=&gt;</span> <span class="number">1</span><span class="operator">);</span>
    <span class="comment"># ...</span>
    <span class="keyword">my</span> <span class="variable">@sheets</span> <span class="operator">=</span> <span class="variable">@</span><span class="operator">{</span><span class="variable">$Excel</span><span class="operator">-&gt;</span><span class="variable">Worksheets</span><span class="operator">-&gt;</span><span class="operator">{</span><span class="string">_NewEnum</span><span class="operator">}}</span><span class="operator">;</span>
</pre>
</dd>
<dd>
<p>In normal application code, this would be better written as:</p>
</dd>
<dd>
<pre>
    <span class="keyword">use</span> <span class="variable">Win32::OLE</span> <span class="string">qw(in)</span><span class="operator">;</span>
    <span class="comment"># ...</span>
    <span class="keyword">my</span> <span class="variable">@sheets</span> <span class="operator">=</span> <span class="variable">in</span> <span class="variable">$Excel</span><span class="operator">-&gt;</span><span class="variable">Worksheets</span><span class="operator">;</span>
</pre>
</dd>
</li>
<dt><strong><a name="item__unique">_Unique</a></strong>

<dd>
<p>The <a href="#item__unique"><code>_Unique</code></a> options guarantees that Win32::OLE will maintain a
one-to-one mapping between Win32::OLE objects and the native COM/OLE
objects.  Without this option, you can query the same property twice
and get two different Win32::OLE objects for the same underlying COM
object.</p>
</dd>
<dd>
<p>Using a unique proxy makes life easier for tree traversal algorithms
to recognize they already visited a particular node.  This option
comes at a price: Win32::OLE has to maintain a global hash of all
outstanding objects and their corresponding proxies.  Identity checks
on COM objects can also be expensive if the objects reside
out-of-process or even on a different computer.  Therefore this option

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?