⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ch06_03.htm

📁 编程珍珠,里面很多好用的代码,大家可以参考学习呵呵,
💻 HTM
字号:
<html><head><title>Passing References (Programming Perl)</title><!-- STYLESHEET --><link rel="stylesheet" type="text/css" href="../style/style1.css"><!-- METADATA --><!--Dublin Core Metadata--><meta name="DC.Creator" content=""><meta name="DC.Date" content=""><meta name="DC.Format" content="text/xml" scheme="MIME"><meta name="DC.Generator" content="XSLT stylesheet, xt by James Clark"><meta name="DC.Identifier" content=""><meta name="DC.Language" content="en-US"><meta name="DC.Publisher" content="O'Reilly &amp; Associates, Inc."><meta name="DC.Source" content="" scheme="ISBN"><meta name="DC.Subject.Keyword" content=""><meta name="DC.Title" content="Passing References"><meta name="DC.Type" content="Text.Monograph"></head><body><!-- START OF BODY --><!-- TOP BANNER --><img src="gifs/smbanner.gif" usemap="#banner-map" border="0" alt="Book Home"><map name="banner-map"><AREA SHAPE="RECT" COORDS="0,0,466,71" HREF="index.htm" ALT="Programming Perl"><AREA SHAPE="RECT" COORDS="467,0,514,18" HREF="jobjects/fsearch.htm" ALT="Search this book"></map><!-- TOP NAV BAR --><div class="navbar"><table width="515" border="0"><tr><td align="left" valign="top" width="172"><a href="ch06_02.htm"><img src="../gifs/txtpreva.gif" alt="Previous" border="0"></a></td><td align="center" valign="top" width="171"><a href="ch06_01.htm">Chapter 6: Subroutines</a></td><td align="right" valign="top" width="172"><a href="ch06_04.htm"><img src="../gifs/txtnexta.gif" alt="Next" border="0"></a></td></tr></table></div><hr width="515" align="left"><!-- SECTION BODY --><h2 class="sect1">6.3. Passing References</h2><a name="INDEX-1839"></a><a name="INDEX-1840"></a><a name="INDEX-1841"></a><p><a name="INDEX-1842"></a>If you want to pass more than one array or hash into or out of afunction, and you want them to maintain their integrity, then you'll needto use an explicit pass-by-reference mechanism. Before youdo that, you need to understand references as detailed in <a href="ch08_01.htm">Chapter 8, "References"</a>.  This section may not make much sense to youotherwise.  But hey, you can always look at the pictures...</p><p>Here are a few simple examples.  First, let's define a function thatexpects a reference to an array.  When the array is large, it's muchfaster to pass it in as a single reference than a long list of values:<blockquote><pre class="programlisting">$total = sum ( \@a );sub sum {    my ($aref)  = @_;    my ($total) = 0;    foreach (@$aref) { $total += $_ }    return $total;}</pre></blockquote>Let's pass in several arrays to a function and have it <tt class="literal">pop</tt> each ofthem, returning a new list of all their former last elements:<blockquote><pre class="programlisting">@tailings = popmany ( \@a, \@b, \@c, \@d );sub popmany {    my @retlist = ();    for my $aref (@_) {        push @retlist, pop @$aref;    }    return @retlist;}</pre></blockquote><a name="INDEX-1843"></a><a name="INDEX-1844"></a></p><p>Here's how you might write a function that does a kind of setintersection by returning a list of keys occurring in all the hashespassed to it:<blockquote><pre class="programlisting">@common = inter( \%foo, \%bar, \%joe );sub inter {    my %seen;    for my $href (@_) {        while (my $k = each %$href ) {            $seen{$k}++;        }    }    return grep { $seen{$_} == @_ } keys %seen;}</pre></blockquote><a name="INDEX-1845"></a>So far, we're just using the normal list return mechanism.  What happensif you want to pass or return a hash?  Well, if you're only using oneof them, or you don't mind them concatenating, then the normal callingconvention is okay, although a little expensive.</p><p>As we explained earlier, where people get into trouble is here:<blockquote><pre class="programlisting">(@a, @b) = func(@c, @d);</pre></blockquote>or here:<blockquote><pre class="programlisting">(%a, %b) = func(%c, %d);</pre></blockquote>That syntax simply won't work.  It just sets <tt class="literal">@a</tt> or <tt class="literal">%a</tt> and clears<tt class="literal">@b</tt> or <tt class="literal">%b</tt>.  Plus the function doesn't get two separate arrays orhashes as arguments: it gets one long list in <tt class="literal">@_</tt>, as always.</p><p><a name="INDEX-1846"></a>You may want to arrange for your functions to use references for bothinput and output.  Here's a function that takes two array referencesas arguments and returns the two array references ordered by the number ofelements they have in them:<blockquote><pre class="programlisting">($aref, $bref) = func(\@c, \@d);print "@$aref has more than @$bref\n";sub func {    my ($cref, $dref) = @_;    if (@$cref &gt; @$dref) {        return ($cref, $dref);    } else {        return ($dref, $cref);    }}</pre></blockquote>For passing filehandles or directory handles into or out of functions,see the sections "Filehandle References" and <a href="ch08_02.htm#ch08-sect-str">Section 6.2.5, "Symbol Table References"</a> in <a href="ch08_01.htm">Chapter 8, "References"</a>.</p><!-- BOTTOM NAV BAR --><hr width="515" align="left"><div class="navbar"><table width="515" border="0"><tr><td align="left" valign="top" width="172"><a href="ch06_02.htm"><img src="../gifs/txtpreva.gif" alt="Previous" border="0"></a></td><td align="center" valign="top" width="171"><a href="index.htm"><img src="../gifs/txthome.gif" alt="Home" border="0"></a></td><td align="right" valign="top" width="172"><a href="ch06_04.htm"><img src="../gifs/txtnexta.gif" alt="Next" border="0"></a></td></tr><tr><td align="left" valign="top" width="172">6.2. Semantics</td><td align="center" valign="top" width="171"><a href="index/index.htm"><img src="../gifs/index.gif" alt="Book Index" border="0"></a></td><td align="right" valign="top" width="172">6.4. Prototypes</td></tr></table></div><hr width="515" align="left"><!-- LIBRARY NAV BAR --><img src="../gifs/smnavbar.gif" usemap="#library-map" border="0" alt="Library Navigation Links"><p><font size="-1"><a href="copyrght.htm">Copyright &copy; 2001</a> O'Reilly &amp; Associates. All rights reserved.</font></p><map name="library-map"> <area shape="rect" coords="2,-1,79,99" href="../index.htm"><area shape="rect" coords="84,1,157,108" href="../perlnut/index.htm"><area shape="rect" coords="162,2,248,125" href="../prog/index.htm"><area shape="rect" coords="253,2,326,130" href="../advprog/index.htm"><area shape="rect" coords="332,1,407,112" href="../cookbook/index.htm"><area shape="rect" coords="414,2,523,103" href="../sysadmin/index.htm"></map><!-- END OF BODY --></body></html>

⌨️ 快捷键说明

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