📄 ch23_02.htm
字号:
<html><head><title>Automation Methods and Properties (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_01.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_03.htm"><img src="../gifs/txtnexta.gif" alt="Next" border="0" /></a></td></tr></table></div><h2 class="sect1">23.2. Automation Methods and Properties</h2><p>Once you have created an automation object, you can use its methodsor adjust its properties as you require. Automation methods areimplemented as you'd expect with the Perl objectsyntax:</p><blockquote><pre class="code">$obj->some_method(args);</pre></blockquote><p>Automation methods can often take a number of optional parameters.You can pass <tt class="literal">undef</tt> for any unwanted parameters inthe arguments list. For example, you can save a WorkBook in Excelwith <tt class="literal">SaveAs</tt>. Additional settings allow you to addthe WorkBook to the MRU list and create a backup copy:</p><blockquote><pre class="code">$xl->WorkBooks(1)->SaveAs($f, undef, undef, undef, undef, 1, undef, undef, 1);</pre></blockquote><p>For simplification, you can also use just the named parameters youwant to set by passing a reference to a hash containing them. You cando this right in the argument list by creating an anonymous hashreference with <tt class="literal">{}</tt>. The previous example cantherefore be written like this:</p><blockquote><pre class="code">$xl->WorkBooks(1)->SaveAs($f, {AddtoMru => 1, CreateBackup => 1});</pre></blockquote><p>Properties of automation objects are accessed via hash referencenotation on the object. For example:</p><blockquote><pre class="code">$val = $obj->{"property"}; # Get a property value$obj->{"property"} = $val; # Set a property value</pre></blockquote><p>Be aware that properties may not be writable (or even readable). Manyautomation objects have read-only properties and will generate anexception if you try to write to them. You'll needto consult the documentation for the object to find out whichproperties you can safely set.</p><p>You can enumerate the properties of an automation object using thenormal methods for enumerating hashes, which are<tt class="literal">keys</tt> and <tt class="literal">each</tt>.Here's how you can print the properties and valuescontained within an object:</p><blockquote><pre class="code">$xl = Win32::OLE->new('Excel.Application', 'Quit');while( ($key,$value) = each %$xl ) { print "$key=$value\n";}</pre></blockquote><a name="perlnut2-CHP-23-SECT-2.1" /><div class="sect2"><h3 class="sect2">23.2.1. Win32::OLE Methods</h3><p>Win32::OLE defines a couple of its own methods for dealing with theautomation interface. These are not automation-defined methods,although they look the same. If a given method is not defined inWin32::OLE, the method call is dispatched to the automation object.If the method doesn't exist there, you will get anOLE error.</p><p>The following methods are defined by Win32::OLE.</p><a name="INDEX-3444" /><div class="refentry"><table width="515" border="0" cellpadding="5"><tr><td align="left"><font size="+1"><b>Invoke</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">obj</em>->Invoke(<em class="replaceable">method</em>, <em class="replaceable">args</em>)</pre><p><a name="INDEX-3444" />This object method calls the given<em class="replaceable"><tt>method</tt></em> for<tt class="literal">$</tt><em class="replaceable"><tt>obj</tt></em> with<em class="replaceable"><tt>args</tt></em> as arguments. It is useful forinvoking methods that would interfere with predefined names in Perl,or methods that contain characters that Perl can'trecognize. You can also use <tt class="literal">Invoke</tt> to call anobject's default method by using either<tt class="literal">undef</tt> or an empty string("") as the first argument.</p></div><a name="INDEX-3445" /><div class="refentry"><table width="515" border="0" cellpadding="5"><tr><td align="left"><font size="+1"><b>LastError</b></font></td><td align="right"><i></i></td></tr></table><hr width="515" size="3" noshade="true" align="left" color="black" /><pre>Win32::OLE->LastError( )</pre><p><a name="INDEX-3445" />This class method returns thelast OLE error. In a numeric context, the error number is returned,while in a string context, the error message is returned.</p></div><a name="INDEX-3446" /><div class="refentry"><table width="515" border="0" cellpadding="5"><tr><td align="left"><font size="+1"><b>QueryObjectType</b></font></td><td align="right"><i></i></td></tr></table><hr width="515" size="3" noshade="true" align="left" color="black" /><pre>Win32::OLE->QueryObjectType($<em class="replaceable">obj</em>)</pre><p><a name="INDEX-3446" />This classmethod returns the type of object queried(<tt class="literal">$</tt><em class="replaceable"><tt>obj</tt></em>). In listcontext, a two-element list of the type library name and the classname of the object is returned. In scalar context, just the classname is returned.</p></div></div><a name="perlnut2-CHP-23-SECT-2.2" /><div class="sect2"><h3 class="sect2">23.2.2. Win32::OLE Functions</h3><p>The following functions are defined by Win32::OLE. They are notexported by default.</p><a name="INDEX-3447" /><div class="refentry"><table width="515" border="0" cellpadding="5"><tr><td align="left"><font size="+1"><b>in</b></font></td><td align="right"><i></i></td></tr></table><hr width="515" size="3" noshade="true" align="left" color="black" /><pre>in($<em class="replaceable">coll</em>)</pre><p><a name="INDEX-3447" />Returns a list of all the membersof a collection referenced by the collection object<tt class="literal">$</tt><em class="replaceable"><tt>coll</tt></em>. Same as<tt class="literal">Win32::OLE::Enum->All( )</tt>.</p></div><a name="INDEX-3448" /><div class="refentry"><table width="515" border="0" cellpadding="5"><tr><td align="left"><font size="+1"><b>valof</b></font></td><td align="right"><i></i></td></tr></table><hr width="515" size="3" noshade="true" align="left" color="black" /><pre>valof($<em class="replaceable">obj</em>)</pre><p><a name="INDEX-3448" />Dereferences an automation object(<tt class="literal">$</tt><em class="replaceable"><tt>obj</tt></em>), calls thedefault method of the object, and returns the value.</p></div><a name="INDEX-3449" /><div class="refentry"><table width="515" border="0" cellpadding="5"><tr><td align="left"><font size="+1"><b>with</b></font></td><td align="right"><i></i></td></tr></table><hr width="515" size="3" noshade="true" align="left" color="black" /><pre>with($<em class="replaceable">obj</em>, <em class="replaceable">property1</em> => <em class="replaceable">value1</em>, ...)</pre><p><a name="INDEX-3449" />Sets the values of multipleproperties on an object(<tt class="literal">$</tt><em class="replaceable"><tt>obj</tt></em>). The functioncalls<tt class="literal">$obj->{</tt><em class="emphasis">property</em>}=<em class="replaceable"><tt>value</tt></em>for each property/value pair.</p></div></div><a name="perlnut2-CHP-23-SECT-2.3" /><div class="sect2"><h3 class="sect2">23.2.3. Win32::OLE Class Variables</h3><p>The Win32::OLE module defines certain class variables that setdefault behavior for automation usage:</p><dl><dt><b><tt class="literal">$Win32::OLE::CP</tt></b></dt><dd>Determines the codepage used by all translations between Perl stringsand Unicode strings used by the OLE interface. The default value is<tt class="literal">CP_ACP</tt>, which is the default ANSI codepage. It canalso be set to <tt class="literal">CP_OEMCP</tt>, which is the default OEMcodepage. Both constants are not exported by default.</p></dd><dt><b><tt class="literal">$Win32::OLE::LCID</tt></b></dt><dd>Controls the locale identifier used for all OLE calls. It is set to<tt class="literal">LOCALE_NEUTRAL</tt> by default. Check the Win32 modulefor other locale-related information.</p></dd><dt><b><tt class="literal">$Win32::OLE::Warn</tt></b></dt><dd>Determines the behavior of the Win32::OLE module when an errorhappens. Valid values are:</p><dl><dt><b><tt class="literal">0</tt></b></dt><dd>Ignores error, returns <tt class="literal">undef</tt>.</p></dd><dt><b><tt class="literal">1</tt></b></dt><dd>Uses <tt class="literal">Carp::carp</tt> if <tt class="literal">$^W</tt> is set(<em class="emphasis">-w</em> option).</p></dd><dt><b><tt class="literal">2</tt></b></dt><dd>Always uses <tt class="literal">Carp::carp</tt>.</p></dd><dt><b><tt class="literal">3</tt></b></dt><dd>Uses <tt class="literal">Carp::croak</tt>.</p></dd></dl><p>The error number and message (without <tt class="literal">Carp</tt>line/module info) are also available through the<tt class="literal">Win32::OLE->LastError</tt> method.</p></dd></dl></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_01.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_03.htm"><img src="../gifs/txtnexta.gif" alt="Next" border="0" /></a></td></tr><tr><td align="left" valign="top" width="228">23. OLE Automation</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.3. Win32::OLE::Enum</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 + -