📄 ch13_08.htm
字号:
<HTML><HEAD><TITLE>Recipe 13.7. Calling Methods Indirectly (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:42:23Z"><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="ch13_01.htm"TITLE="13. Classes, Objects, and Ties"><LINKREL="prev"HREF="ch13_07.htm"TITLE="13.6. Cloning Objects"><LINKREL="next"HREF="ch13_09.htm"TITLE="13.8. Determining Subclass Membership"></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="ch13_07.htm"TITLE="13.6. Cloning Objects"><IMGSRC="../gifs/txtpreva.gif"ALT="Previous: 13.6. Cloning Objects"BORDER="0"></A></TD><TDALIGN="CENTER"VALIGN="TOP"WIDTH="228"><B><FONTFACE="ARIEL,HELVETICA,HELV,SANSERIF"SIZE="-1"><ACLASS="chapter"REL="up"HREF="ch13_01.htm"TITLE="13. Classes, Objects, and Ties"></A></FONT></B></TD><TDALIGN="RIGHT"VALIGN="TOP"WIDTH="228"><ACLASS="sect1"HREF="ch13_09.htm"TITLE="13.8. Determining Subclass Membership"><IMGSRC="../gifs/txtnexta.gif"ALT="Next: 13.8. Determining Subclass Membership"BORDER="0"></A></TD></TR></TABLE></DIV><DIVCLASS="sect1"><H2CLASS="sect1"><ACLASS="title"NAME="ch13-15749">13.7. Calling Methods Indirectly</A></H2><DIVCLASS="sect2"><H3CLASS="sect2"><ACLASS="title"NAME="ch13-pgfId-1009">Problem<ACLASS="indexterm"NAME="ch13-idx-1000004543-0"></A></A></H3><PCLASS="para">You want to call a method by a name that isn't known until run time.</P></DIV><DIVCLASS="sect2"><H3CLASS="sect2"><ACLASS="title"NAME="ch13-pgfId-1015">Solution</A></H3><PCLASS="para">Store the method name as a string in a scalar variable and use it where you would use the real method name to the right of the arrow operator:</P><PRECLASS="programlisting">$methname = "flicker";$obj->$methname(10); # calls $ob->flicker(10);# call three methods on the object, by nameforeach $m ( qw(start run stop) ) { $obj-><CODECLASS="literal">$m();</CODE>} </PRE></DIV><DIVCLASS="sect2"><H3CLASS="sect2"><ACLASS="title"NAME="ch13-pgfId-1035">Discussion</A></H3><PCLASS="para">Sometimes you need to call a method whose name you've stored somewhere. You can't take the address of a method, but you can store its name. If you have a scalar variable <CODECLASS="literal">$meth</CODE> containing the method name, call the method on an object <CODECLASS="literal">$crystal</CODE> with <CODECLASS="literal">$crystal->$meth()</CODE>.</P><PRECLASS="programlisting">@methods = qw(name rank serno);%his_info = map { $_ => $ob->$_() } @methods;# same as this:%his_info = ( 'name' => $ob-><CODECLASS="literal">name()</CODE>, 'rank' => $ob-><CODECLASS="literal">rank()</CODE>, 'serno' => $ob-><CODECLASS="literal">serno()</CODE>,);</PRE><PCLASS="para">If you're desperate to devise a way to get a method's address, you should try to rethink your algorithm. For example, instead of incorrectly taking <CODECLASS="literal">\$ob->method()</CODE>, which simply applies the backslash to that method's return value or values, do this:</P><PRECLASS="programlisting">my $fnref = sub { $ob->method(@_) };</PRE><PCLASS="para">Now when it's time to call that indirectly, you would use:</P><PRECLASS="programlisting">$fnref->(10, "fred");</PRE><PCLASS="para">and have it correctly really call:</P><PRECLASS="programlisting">$obj->method(10, "fred");</PRE><PCLASS="para">This works even if <CODECLASS="literal">$ob</CODE> has gone out of scope. This solution is much cleaner.</P><PCLASS="para">The code reference returned by the UNIVERSAL <CODECLASS="literal">can()</CODE><ACLASS="indexterm"NAME="ch13-idx-1000004550-0"></A> method should probably not be used as an indirect method call. That's because you have no reason to believe that this will be a valid method when applied to an object of an arbitrary class.</P><PCLASS="para">For example, this is highly dubious code:</P><PRECLASS="programlisting">$obj->can('method_name')->($obj_target, @arguments) if $obj_target->isa( ref $obj );</PRE><PCLASS="para">The problem is that the code ref returned by <CODECLASS="literal">can</CODE> might not be a valid method to be called on <CODECLASS="literal">$obj2</CODE>. It's probably safest to only test the <CODECLASS="literal">can()</CODE> method in a boolean expression.<ACLASS="indexterm"NAME="ch13-idx-1000004545-0"></A></P></DIV><DIVCLASS="sect2"><H3CLASS="sect2"><ACLASS="title"NAME="ch13-pgfId-1000004355">See Also</A></H3><PCLASS="para"><ICLASS="filename">perlobj </I>(1); <ACLASS="xref"HREF="ch11_09.htm"TITLE="Creating References to Methods">Recipe 11.8</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="ch13_07.htm"TITLE="13.6. Cloning Objects"><IMGSRC="../gifs/txtpreva.gif"ALT="Previous: 13.6. Cloning Objects"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="ch13_09.htm"TITLE="13.8. Determining Subclass Membership"><IMGSRC="../gifs/txtnexta.gif"ALT="Next: 13.8. Determining Subclass Membership"BORDER="0"></A></TD></TR><TR><TDALIGN="LEFT"VALIGN="TOP"WIDTH="228">13.6. Cloning Objects</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">13.8. Determining Subclass Membership</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 + -