📄 ch23_03.htm
字号:
<html><head><title>Win32::OLE::Enum (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_02.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_04.htm"><img src="../gifs/txtnexta.gif" alt="Next" border="0" /></a></td></tr></table></div><h2 class="sect1">23.3. Win32::OLE::Enum</h2><p><a name="INDEX-3450" /><a name="INDEX-3451" />The Win32::OLE::Enum module providesspecial support for collections. Collections are special automationdata types that contain an array of objects or data. A collectionsupports enumeration; you can iterate through each item through astandard interface.</p><p>Collection objects should always provide a <tt class="literal">Count</tt>property (the number of items in the collection) and an<tt class="literal">Item</tt> method. The <tt class="literal">Item</tt> method isused to access a particular collection item using a subscript, whichmay be an integer or a string, depending on the server. Collectionobjects may also optionally contain an <tt class="literal">Add</tt> and a<tt class="literal">Remove</tt> method.</p><p>Collection objects also support a standard COM interface(IEnumVARIANT) that allows you to enumerate each item in acollection. It defines methods that let you advance the iteration tothe next item, skip a given item, restart the enumeration, and createa new copy of the iterator. While all servers are supposed to providethis interface, some servers don't implement all ofthe methods (often <tt class="literal">Reset</tt> and<tt class="literal">Clone</tt> are not implemented).</p><p><a name="INDEX-3452" /><a name="INDEX-3453" />Win32::OLE::Enum definesthese methods for enumerating collections. The collection objectshould provide the <tt class="literal">Count</tt> and<tt class="literal">Item</tt> methods, which are often all you need to useon collections. For example:</p><blockquote><pre class="code">$cnt = $coll->Count( );if( $cnt) { $obj = $coll->Item(0); $obj->do_something( );}</pre></blockquote><p><tt class="literal">Count</tt> will tell you the number of items in thecollection, and <tt class="literal">Item</tt> will return the desired itemas a Win32::OLE object.</p><p>For the enumeration methods, you need to create an enumeration objectfor the collection object:</p><blockquote><pre class="code">$coll = $obj->some_coll( );$enum = Win32::OLE::Enum->new($coll);</pre></blockquote><p>Now you can use the <a name="INDEX-3454" />enumeration methods on the object.</p><a name="perlnut2-CHP-23-SECT-3.1" /><div class="sect2"><h3 class="sect2">23.3.1. Win32::OLE::Enum Methods</h3><p>The following methods are defined in Win32::OLE::Enum.</p><a name="INDEX-3455" /><div class="refentry"><table width="515" border="0" cellpadding="5"><tr><td align="left"><font size="+1"><b>new</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::Enum->new($<em class="replaceable">obj</em>)</pre><p><a name="INDEX-3455" />Creates a new Win32::OLE::Enum object.Provides it with a collection object or an existing Enum object, inwhich case it calls <tt class="literal">Clone</tt>.</p></div><a name="INDEX-3456" /><div class="refentry"><table width="515" border="0" cellpadding="5"><tr><td align="left"><font size="+1"><b>All</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">Enum</em>->All( )</pre><p><a name="INDEX-3456" />Returns a list of allobjects in the collection. Note that to use <tt class="literal">All</tt>again, you need to first call <tt class="literal">Reset</tt>.</p></div><a name="INDEX-3457" /><div class="refentry"><table width="515" border="0" cellpadding="5"><tr><td align="left"><font size="+1"><b>Clone</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">Enum</em>->Clone( )</pre><p><a name="INDEX-3457" />Returns a copy of thecurrent iterator. This method is supposed to maintain the sameiteration position, if possible, but may be unimplemented.</p></div><a name="INDEX-3458" /><div class="refentry"><table width="515" border="0" cellpadding="5"><tr><td align="left"><font size="+1"><b>Next</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">Enum</em>->Next([<em class="replaceable">count</em>])</pre><p><a name="INDEX-3458" />Returns the next item inthe collection. You can optionally provide <tt class="literal">Next</tt>with a count (which must be greater than zero), in which case itreturns a list of the next count items. Note that if you provide ascalar context in conjunction with a count, you'llget only the last item in the list of returned items.<tt class="literal">Next</tt> returns <tt class="literal">undef</tt> if it iscurrently on the last item in the collection.</p></div><a name="INDEX-3459" /><div class="refentry"><table width="515" border="0" cellpadding="5"><tr><td align="left"><font size="+1"><b>Reset</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">Enum</em>->Reset( )</pre><p><a name="INDEX-3459" />Restarts the enumeration with the firstitem in the collection. <tt class="literal">Reset</tt> returns true if itsucceeds, false if it fails. Note that this method may beunimplemented.</p></div><a name="INDEX-3460" /><div class="refentry"><table width="515" border="0" cellpadding="5"><tr><td align="left"><font size="+1"><b>Skip</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">Enum</em>->Skip([<em class="replaceable">count</em>])</pre><p><a name="INDEX-3460" />Skips the next<em class="replaceable"><tt>count</tt></em> number of items of the enumeration(again, <em class="replaceable"><tt>count</tt></em> must be positive anddefaults to <tt class="literal">1</tt>). <tt class="literal">Skip</tt> returnsfalse if there are not at least <em class="replaceable"><tt>count</tt></em>items left.</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_02.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_04.htm"><img src="../gifs/txtnexta.gif" alt="Next" border="0" /></a></td></tr><tr><td align="left" valign="top" width="228">23.2. Automation Methods and Properties</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.4. Win32::OLE::Variant</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 + -