📄 ch10_05.htm
字号:
<HTML><HEAD><TITLE>Recipe 10.4. Determining Current Function Name (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:39:38Z"><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_04.htm"TITLE="10.3. Creating Persistent Private Variables"><LINKREL="next"HREF="ch10_06.htm"TITLE="10.5. Passing Arrays and Hashes by Reference"></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_04.htm"TITLE="10.3. Creating Persistent Private Variables"><IMGSRC="../gifs/txtpreva.gif"ALT="Previous: 10.3. Creating Persistent Private Variables"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_06.htm"TITLE="10.5. Passing Arrays and Hashes by Reference"><IMGSRC="../gifs/txtnexta.gif"ALT="Next: 10.5. Passing Arrays and Hashes by Reference"BORDER="0"></A></TD></TR></TABLE></DIV><DIVCLASS="sect1"><H2CLASS="sect1"><ACLASS="title"NAME="ch10-31648">10.4. Determining Current Function Name</A></H2><DIVCLASS="sect2"><H3CLASS="sect2"><ACLASS="title"NAME="ch10-pgfId-326">Problem</A></H3><PCLASS="para"><ACLASS="indexterm"NAME="ch10-idx-1000004665-0"></A><ACLASS="indexterm"NAME="ch10-idx-1000004665-1"></A>You want to determine the name of the currently running function. This is useful for creating error messages that don't need to be changed if you copy and paste the subroutine code.</P></DIV><DIVCLASS="sect2"><H3CLASS="sect2"><ACLASS="title"NAME="ch10-pgfId-332">Solution</A></H3><PCLASS="para">Use the <CODECLASS="literal">caller</CODE><ACLASS="indexterm"NAME="ch10-idx-1000004666-0"></A> function:</P><PRECLASS="programlisting">$this_function = (caller(0))[3];</PRE></DIV><DIVCLASS="sect2"><H3CLASS="sect2"><ACLASS="title"NAME="ch10-pgfId-340">Discussion</A></H3><PCLASS="para">Code can always find the current line number in the special symbol <CODECLASS="literal">__LINE__ </CODE><ACLASS="indexterm"NAME="ch10-idx-1000004672-0"></A><ACLASS="indexterm"NAME="ch10-idx-1000004672-1"></A><ACLASS="indexterm"NAME="ch10-idx-1000004672-2"></A>, the current file in <CODECLASS="literal">__FILE__</CODE> , and the current package in <CODECLASS="literal">__PACKAGE__ </CODE>. But there's no such symbol for the current subroutine name, let alone the name of the one that called this subroutine.</P><PCLASS="para">The built-in function <CODECLASS="literal">caller</CODE> handles all of these. In scalar context it returns the calling function's package name. But in list context, it returns a wealth of information. You can also pass it a number indicating how many frames (nested subroutine calls) back you'd like information about: 0 is your own function, 1 is your caller, and so on.</P><PCLASS="para">Here's the full syntax, where <CODECLASS="literal">$i</CODE> is how far back you're interested in:</P><PRECLASS="programlisting">($package, $filename, $line, $subr, $has_args, $wantarray )= caller($i);# 0 1 2 3 4 5</PRE><PCLASS="para">Here's what each of those return values means:</P><DLCLASS="variablelist"><DTCLASS="term"><CODECLASS="literal">$package </CODE></DT><DDCLASS="listitem"><PCLASS="para">The package that the code was compiled in.</P></DD><DTCLASS="term"><CODECLASS="literal">$filename </CODE></DT><DDCLASS="listitem"><PCLASS="para">The name of the file the code was compiled in, reporting <CODECLASS="literal">-e</CODE> if launched from the command-line switch of the same name, or <CODECLASS="literal">-</CODE> if the script was read from STDIN.</P></DD><DTCLASS="term"><CODECLASS="literal">$line </CODE></DT><DDCLASS="listitem"><PCLASS="para">The line number that frame was called from.</P></DD><DTCLASS="term"><CODECLASS="literal">$subr </CODE></DT><DDCLASS="listitem"><PCLASS="para">The name of that frame's function, including its package. Closures return names like <CODECLASS="literal">main::__ANON__ </CODE>, which are not callable. In <CODECLASS="literal">eval</CODE> it returns <CODECLASS="literal">"(eval)"</CODE>.</P></DD><DTCLASS="term"><CODECLASS="literal">$has_args </CODE></DT><DDCLASS="listitem"><PCLASS="para">Whether the function was called with arguments.</P></DD><DTCLASS="term"><CODECLASS="literal">$wantarray</CODE> </DT><DDCLASS="listitem"><PCLASS="para">The value the <CODECLASS="literal">wantarray</CODE> function would return for that stack frame; either true, false but defined, or else undefined (respectively). This tells you whether the function was called in list, scalar, or void context.</P></DD></DL><PCLASS="para">Rather than using <CODECLASS="literal">caller</CODE><ACLASS="indexterm"NAME="ch10-idx-1000004673-0"></A><ACLASS="indexterm"NAME="ch10-idx-1000004673-1"></A><ACLASS="indexterm"NAME="ch10-idx-1000004673-2"></A> directly as in the solution, you might want to write functions instead:</P><PRECLASS="programlisting">$me = whoami();$him = whowasi();sub whoami { (caller(1))[3] }sub whowasi { (caller(2))[3] }</PRE><PCLASS="para">These use arguments 1 and 2 for parent and grandparent functions because the call to <CODECLASS="literal">whoami</CODE> or <CODECLASS="literal">whowasi</CODE> would be number 0.<ACLASS="indexterm"NAME="ch10-idx-1000004668-0"></A><ACLASS="indexterm"NAME="ch10-idx-1000004668-1"></A></P></DIV><DIVCLASS="sect2"><H3CLASS="sect2"><ACLASS="title"NAME="ch10-pgfId-1000004848">See Also</A></H3><PCLASS="para">The <ACLASS="olink"HREF="../prog/ch03_191.htm"><CODECLASS="literal">wantarray</CODE></A> and <ACLASS="olink"HREF="../prog/ch03_010.htm"><CODECLASS="literal">caller</CODE></A> functions in <ACLASS="olink"HREF="../prog/ch03_01.htm">Chapter 3</A> of <ACLASS="citetitle"HREF="../prog/index.htm"TITLE="Programming Perl"><CITECLASS="citetitle">Programming Perl</CITE></A> and in <ICLASS="filename">perlfunc </I>(1); <ACLASS="xref"HREF="ch10_07.htm"TITLE="Detecting Return Context">Recipe 10.6</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_04.htm"TITLE="10.3. Creating Persistent Private Variables"><IMGSRC="../gifs/txtpreva.gif"ALT="Previous: 10.3. Creating Persistent Private Variables"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_06.htm"TITLE="10.5. Passing Arrays and Hashes by Reference"><IMGSRC="../gifs/txtnexta.gif"ALT="Next: 10.5. Passing Arrays and Hashes by Reference"BORDER="0"></A></TD></TR><TR><TDALIGN="LEFT"VALIGN="TOP"WIDTH="228">10.3. Creating Persistent Private Variables</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.5. Passing Arrays and Hashes by Reference</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 + -