📄 ch10_16.htm
字号:
<HTML><HEAD><TITLE>Recipe 10.15. Trapping Undefined Function Calls with AUTOLOAD (Perl Cookbook)</TITLE><METANAME="DC.title"CONTENT="Perl Cookbook"><METANAME="DC.creator"CONTENT="Tom Christiansen & Nathan Torkington"><METANAME="DC.publisher"CONTENT="O'Reilly & Associates, Inc."><METANAME="DC.date"CONTENT="1999-07-02T01:40:07Z"><METANAME="DC.type"CONTENT="Text.Monograph"><METANAME="DC.format"CONTENT="text/html"SCHEME="MIME"><METANAME="DC.source"CONTENT="1-56592-243-3"SCHEME="ISBN"><METANAME="DC.language"CONTENT="en-US"><METANAME="generator"CONTENT="Jade 1.1/O'Reilly DocBook 3.0 to HTML 4.0"><LINKREV="made"HREF="mailto:online-books@oreilly.com"TITLE="Online Books Comments"><LINKREL="up"HREF="ch10_01.htm"TITLE="10. Subroutines"><LINKREL="prev"HREF="ch10_15.htm"TITLE="10.14. Redefining a Function"><LINKREL="next"HREF="ch10_17.htm"TITLE="10.16. Nesting Subroutines"></HEAD><BODYBGCOLOR="#FFFFFF"><img alt="Book Home" border="0" src="gifs/smbanner.gif" usemap="#banner-map" /><map name="banner-map"><area shape="rect" coords="1,-2,616,66" href="index.htm" alt="Perl Cookbook"><area shape="rect" coords="629,-11,726,25" href="jobjects/fsearch.htm" alt="Search this book" /></map><div class="navbar"><p><TABLEWIDTH="684"BORDER="0"CELLSPACING="0"CELLPADDING="0"><TR><TDALIGN="LEFT"VALIGN="TOP"WIDTH="228"><ACLASS="sect1"HREF="ch10_15.htm"TITLE="10.14. Redefining a Function"><IMGSRC="../gifs/txtpreva.gif"ALT="Previous: 10.14. Redefining a Function"BORDER="0"></A></TD><TDALIGN="CENTER"VALIGN="TOP"WIDTH="228"><B><FONTFACE="ARIEL,HELVETICA,HELV,SANSERIF"SIZE="-1"><ACLASS="chapter"REL="up"HREF="ch10_01.htm"TITLE="10. Subroutines"></A></FONT></B></TD><TDALIGN="RIGHT"VALIGN="TOP"WIDTH="228"><ACLASS="sect1"HREF="ch10_17.htm"TITLE="10.16. Nesting Subroutines"><IMGSRC="../gifs/txtnexta.gif"ALT="Next: 10.16. Nesting Subroutines"BORDER="0"></A></TD></TR></TABLE></DIV><DIVCLASS="sect1"><H2CLASS="sect1"><ACLASS="title"NAME="ch10-42363">10.15. Trapping Undefined Function Calls with AUTOLOAD</A></H2><DIVCLASS="sect2"><H3CLASS="sect2"><ACLASS="title"NAME="ch10-pgfId-1158">Problem</A></H3><PCLASS="para"><ACLASS="indexterm"NAME="ch10-idx-1000004745-0"></A><ACLASS="indexterm"NAME="ch10-idx-1000004745-1"></A><ACLASS="indexterm"NAME="ch10-idx-1000004745-2"></A><ACLASS="indexterm"NAME="ch10-idx-1000004745-3"></A><ACLASS="indexterm"NAME="ch10-idx-1000004745-4"></A>You want to intercept calls to undefined functions so you can handle them gracefully.</P></DIV><DIVCLASS="sect2"><H3CLASS="sect2"><ACLASS="title"NAME="ch10-pgfId-1164">Solution</A></H3><PCLASS="para">Declare a function called <CODECLASS="literal">AUTOLOAD</CODE> for the package whose undefined function calls you'd like to trap. While running, that package's <CODECLASS="literal">$AUTOLOAD</CODE> variable contains the name of the undefined function being called.</P></DIV><DIVCLASS="sect2"><H3CLASS="sect2"><ACLASS="title"NAME="ch10-pgfId-1170">Discussion</A></H3><PCLASS="para">Another strategy for creating similar functions is to use a proxy function. If you call an undefined function, instead of automatically raising an exception, you can trap the call. If the function's package has a function named <CODECLASS="literal">AUTOLOAD</CODE>, then this function is called in its place, with the special package global <CODECLASS="literal">$AUTOLOAD</CODE> set to the fully qualified function name. The <CODECLASS="literal">AUTOLOAD</CODE> subroutine can then do whatever that function would do.</P><PRECLASS="programlisting">sub AUTOLOAD { use vars qw($AUTOLOAD); my $color = $AUTOLOAD; $color =~ s/.*:://; return "<FONT COLOR='$color'>@_</FONT>";} #note: sub chartreuse isn't defined.print chartreuse("stuff");</PRE><PCLASS="para">When the nonexistent <CODECLASS="literal">main::chartreuse</CODE> function is called, rather than raising an exception, <CODECLASS="literal">main::AUTOLOAD</CODE> is called with the same arguments as you passed <CODECLASS="literal">chartreuse </CODE>. The package variable <CODECLASS="literal">$AUTOLOAD</CODE> would contain the string <CODECLASS="literal">main::chartreuse</CODE> because that's the function it's proxying.</P><PCLASS="para">The technique using typeglob assignments shown in <ACLASS="xref"HREF="ch10_15.htm"TITLE="Redefining a Function">Recipe 10.14</A> is faster and more flexible than using <CODECLASS="literal">AUTOLOAD</CODE>. It's faster because you don't have to run the copy and substitute. It's more flexible because it lets you do this:</P><PRECLASS="programlisting">{ local *yellow = \&violet; local (*red, *green) = (\&green, \&red); print_stuff();} </PRE><PCLASS="para">While <CODECLASS="literal">print_stuff()</CODE> is running, or while in any functions it calls, anything printed in yellow will come out violet, and the red and green texts will exchange colors.</P><PCLASS="para">Aliasing subroutines like this won't handle calls to undefined subroutines. <CODECLASS="literal">AUTOLOAD</CODE> does.<ACLASS="indexterm"NAME="ch10-idx-1000004747-0"></A><ACLASS="indexterm"NAME="ch10-idx-1000004747-1"></A><ACLASS="indexterm"NAME="ch10-idx-1000004747-2"></A><ACLASS="indexterm"NAME="ch10-idx-1000004747-3"></A><ACLASS="indexterm"NAME="ch10-idx-1000004747-4"></A></P></DIV><DIVCLASS="sect2"><H3CLASS="sect2"><ACLASS="title"NAME="ch10-pgfId-1210">See Also</A></H3><PCLASS="para">The section on <ACLASS="olink"HREF="../prog/ch05_01.htm#PERL2-CH-5-SECT-1.3">"Autoloading"</A> in <ACLASS="olink"HREF="../prog/ch05_01.htm">Chapter 5</A> of <ACLASS="citetitle"HREF="../prog/index.htm"TITLE="Programming Perl"><CITECLASS="citetitle">Programming Perl</CITE></A> and in <ICLASS="filename">perlsub </I>(1); the documentation for the standard modules AutoLoader and AutoSplit, also in <ACLASS="olink"HREF="../prog/ch07_01.htm">Chapter 7</A> of <ACLASS="citetitle"HREF="../prog/index.htm"TITLE="Programming Perl"><CITECLASS="citetitle">Programming Perl</CITE></A>; <ACLASS="xref"HREF="ch10_13.htm"TITLE="Handling Exceptions">Recipe 10.12</A>; <ACLASS="xref"HREF="ch12_11.htm"TITLE="Speeding Up Module Loading with Autoloader">Recipe 12.10</A>, <ACLASS="xref"HREF="ch13_12.htm"TITLE="Generating Attribute Methods Using AUTOLOAD">Recipe 13.11</A></P></DIV></DIV><DIVCLASS="htmlnav"><P></P><HRALIGN="LEFT"WIDTH="684"TITLE="footer"><TABLEWIDTH="684"BORDER="0"CELLSPACING="0"CELLPADDING="0"><TR><TDALIGN="LEFT"VALIGN="TOP"WIDTH="228"><ACLASS="sect1"HREF="ch10_15.htm"TITLE="10.14. Redefining a Function"><IMGSRC="../gifs/txtpreva.gif"ALT="Previous: 10.14. Redefining a Function"BORDER="0"></A></TD><TDALIGN="CENTER"VALIGN="TOP"WIDTH="228"><ACLASS="book"HREF="index.htm"TITLE="Perl Cookbook"><IMGSRC="../gifs/txthome.gif"ALT="Perl Cookbook"BORDER="0"></A></TD><TDALIGN="RIGHT"VALIGN="TOP"WIDTH="228"><ACLASS="sect1"HREF="ch10_17.htm"TITLE="10.16. Nesting Subroutines"><IMGSRC="../gifs/txtnexta.gif"ALT="Next: 10.16. Nesting Subroutines"BORDER="0"></A></TD></TR><TR><TDALIGN="LEFT"VALIGN="TOP"WIDTH="228">10.14. Redefining a Function</TD><TDALIGN="CENTER"VALIGN="TOP"WIDTH="228"><ACLASS="index"HREF="index/index.htm"TITLE="Book Index"><IMGSRC="../gifs/index.gif"ALT="Book Index"BORDER="0"></A></TD><TDALIGN="RIGHT"VALIGN="TOP"WIDTH="228">10.16. Nesting Subroutines</TD></TR></TABLE><HRALIGN="LEFT"WIDTH="684"TITLE="footer"><FONTSIZE="-1"></DIV<!-- LIBRARY NAV BAR --> <img src="../gifs/smnavbar.gif" usemap="#library-map" border="0" alt="Library Navigation Links"><p> <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 + -