tpj.html
来自「perl教程」· HTML 代码 · 共 676 行 · 第 1/5 页
HTML
676 行
<p>Some server objects (particularly those for Microsoft Office
applications) don't automatically terminate when your program no
longer needs them. They need some kind of Quit method, and that's just
what our second argument is. It can be either a code reference or a
method name to be invoked when the object is destroyed. This lets you
ensure that objects will be properly cleaned up even when the Perl
program dies abnormally.</p>
<p>To access a server object on a different computer, replace the first
argument with a reference to a list of the server name and program ID:</p>
<pre>
<span class="keyword">my</span> <span class="variable">$server</span> <span class="operator">=</span> <span class="variable">Win32::OLE</span><span class="operator">-></span><span class="variable">new</span><span class="operator">(</span><span class="operator">[</span><span class="string">'foo.bar.com'</span><span class="operator">,</span>
<span class="string">'Excel.Application'</span><span class="operator">]</span><span class="operator">);</span>
</pre>
<p>(To get the requisite permissions, you'll need to configure your
security settings with <em>DCOMCNFG.EXE</em>.)</p>
<p>You can also directly attach your program to an already running OLE
server:</p>
<pre>
<span class="keyword">my</span> <span class="variable">$server</span> <span class="operator">=</span> <span class="variable">Win32::OLE</span><span class="operator">-></span><span class="variable">GetActiveObject</span><span class="operator">(</span><span class="string">'Excel.Application'</span><span class="operator">);</span>
</pre>
<p>This fails (returning <a href="../../../lib/Pod/perlfunc.html#item_undef"><code>undef</code></a>) if no server exists, or if the server
refuses the connection for some reason. It is also possible to use a
persistent object moniker (usually a filename) to start the associated
server and load the object into memory:</p>
<pre>
<span class="keyword">my</span> <span class="variable">$doc</span> <span class="operator">=</span> <span class="variable">Win32::OLE</span><span class="operator">-></span><span class="variable">GetObject</span><span class="operator">(</span><span class="string">"MyDocument.Doc"</span><span class="operator">);</span>
</pre>
<p>
</p>
<h2><a name="method_calls">METHOD CALLS</a></h2>
<p>Once you've created one of these server objects, you need to call its
methods to make the OLE objects sing and dance. OLE methods are
invoked just like normal Perl object methods:</p>
<pre>
<span class="variable">$server</span><span class="operator">-></span><span class="variable">Foo</span><span class="operator">(</span><span class="variable">@Arguments</span><span class="operator">);</span>
</pre>
<p>This is a Perl method call - but it also triggers an OLE method call
in the object. After your program executes this statement, the
<code>$server</code> object will execute its <code>Foo()</code> method. The available methods
are typically documented in the application's <em>object model</em>.</p>
<p><strong>Parameters.</strong> By default, all parameters are positional
(e.g. <code>foo($first, $second, $third)</code>) rather than named (e.g.
<code>foo(-name => "Yogi", -title => "Coach")</code>). The required parameters
come first, followed by the optional parameters; if you need to
provide a dummy value for an optional parameter, use undef.</p>
<p>Positional parameters get cumbersome if a method takes a lot of
them. You can use named arguments instead if you go to a little extra
trouble - when the last argument is a reference to a hash, the
key/value pairs of the hash are treated as named parameters:</p>
<pre>
<span class="variable">$server</span><span class="operator">-></span><span class="variable">Foo</span><span class="operator">(</span><span class="variable">$Pos1</span><span class="operator">,</span> <span class="variable">$Pos2</span><span class="operator">,</span> <span class="operator">{</span><span class="string">Name1</span> <span class="operator">=></span> <span class="variable">$Value1</span><span class="operator">,</span>
<span class="string">Name2</span> <span class="operator">=></span> <span class="variable">$Value2</span><span class="operator">}</span><span class="operator">);</span>
</pre>
<p><strong>Foreign Languages and Default Methods.</strong> Sometimes OLE servers use
method and property names that are specific to a non-English
locale. That means they might have non-ASCII characters, which aren't
allowed in Perl variable names. In German, you might see <code>謋fnen</code> used
instead of <code>Open</code>. In these cases, you can use the <code>Invoke()</code> method:</p>
<pre>
<span class="variable">$server</span><span class="operator">-></span><span class="variable">Invoke</span><span class="operator">(</span><span class="string">'謋fnen'</span><span class="operator">,</span> <span class="variable">@Arguments</span><span class="operator">);</span>
</pre>
<p>This is necessary because <code>$Server->謋fnen(@Arguments)</code> is a syntax
error in current versions of Perl.</p>
<p>
</p>
<h2><a name="properties">PROPERTIES</a></h2>
<p>As I said earlier, objects can expose three things to the outside
world: methods, properties, and events. We've covered methods, and
Win32::OLE can't handle events. That leaves properties. But as it
turns out, properties and events are largely interchangeable. Most
methods have corresponding properties, and vice versa.</p>
<p>An object's properties can be accessed with a hash reference:</p>
<pre>
<span class="variable">$server</span><span class="operator">-></span><span class="operator">{</span><span class="string">Bar</span><span class="operator">}</span> <span class="operator">=</span> <span class="variable">$value</span><span class="operator">;</span>
<span class="variable">$value</span> <span class="operator">=</span> <span class="variable">$server</span><span class="operator">-></span><span class="operator">{</span><span class="string">Bar</span><span class="operator">}</span><span class="operator">;</span>
</pre>
<p>This example sets and queries the value of the property named
<code>Bar</code>. You could also have called the object's <code>Bar()</code> method to
achieve the same effect:</p>
<pre>
<span class="variable">$value</span> <span class="operator">=</span> <span class="variable">$server</span><span class="operator">-></span><span class="variable">Bar</span><span class="operator">;</span>
</pre>
<p>However, you can't write the first line as <code>$server->Bar = $value</code>,
because you can't assign to the return value of a method call. In
Visual Basic, OLE automation distinguishes between assigning the name
of an object and assigning its value:</p>
<pre>
Set Object = OtherObject</pre>
<pre>
Let Value = Object</pre>
<p>The <code>Set</code> statement shown here makes <code>Object</code> refer to the same object as
<code>OtherObject</code>. The <code>Let</code> statement copies the value instead. (The value of
an OLE object is what you get when you call the object's default
method.</p>
<p>In Perl, saying <code>$server1 = $server2</code> always creates another reference,
just like the <code>Set</code> in Visual Basic. If you want to assign the value
instead, use the <code>valof()</code> function:</p>
<pre>
<span class="keyword">my</span> <span class="variable">$value</span> <span class="operator">=</span> <span class="variable">valof</span> <span class="variable">$server</span><span class="operator">;</span>
</pre>
<p>This is equivalent to</p>
<pre>
<span class="keyword">my</span> <span class="variable">$value</span> <span class="operator">=</span> <span class="variable">$server</span><span class="operator">-></span><span class="variable">Invoke</span><span class="operator">(</span><span class="string">''</span><span class="operator">);</span>
</pre>
<p>
</p>
<h2><a name="sample_application">SAMPLE APPLICATION</a></h2>
<p>Let's look at how all of this might be used. In Listing: 1 you'll see
<em>T-Bond.pl</em>, a program that uses Win32::OLE for an almost-real world
application.</p>
<p>The developer of this application, Mary Lynch, is a financial futures
broker. Every afternoon, she connects to the Chicago Board of Trade
(CBoT) web site at <a href="http://www.cbot.com">http://www.cbot.com</a> and collects the time and sales
information for U.S. T-bond futures. She wants her program to create a
chart that depicts the data in 15-minute intervals, and then she wants
to record the data in a database for later analysis. Then she wants
her program to send mail to her clients.</p>
<p>Mary's program will use Microsoft Access as a database, Microsoft
Excel to produce the chart, and Lotus Notes to send the mail. It will
all be controlled from a single Perl program using OLE automation. In
this section, we'll go through T-Bond. pl step by step so you can see
how Win32::OLE lets you control these applications.</p>
<p>
</p>
<h2><a name="downloading_a_web_page_with_lwp">DOWNLOADING A WEB PAGE WITH LWP</a></h2>
<p>However, Mary first needs to amass the raw T-bond data by having her
Perl program automatically download and parse a web page. That's the
perfect job for LWP, the libwww-perl bundle available on the CPAN. LWP
has nothing to do with OLE. But this is a real-world application, and
it's just what Mary needs to download her data from the Chicago Board
of Trade.</p>
<pre>
<span class="keyword">use</span> <span class="variable">LWP::Simple</span><span class="operator">;</span>
<span class="keyword">my</span> <span class="variable">$URL</span> <span class="operator">=</span> <span class="string">'http://www.cbot.com/mplex/quotes/tsfut'</span><span class="operator">;</span>
<span class="keyword">my</span> <span class="variable">$text</span> <span class="operator">=</span> <span class="variable">get</span><span class="operator">(</span><span class="string">"$URL/tsf$Contract.htm"</span><span class="operator">);</span>
</pre>
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?