📄 ch13_09.htm
字号:
<HTML><HEAD><TITLE>Recipe 13.8. Determining Subclass Membership (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:24Z"><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_08.htm"TITLE="13.7. Calling Methods Indirectly"><LINKREL="next"HREF="ch13_10.htm"TITLE="13.9. Writing an Inheritable Class"></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_08.htm"TITLE="13.7. Calling Methods Indirectly"><IMGSRC="../gifs/txtpreva.gif"ALT="Previous: 13.7. Calling Methods Indirectly"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_10.htm"TITLE="13.9. Writing an Inheritable Class"><IMGSRC="../gifs/txtnexta.gif"ALT="Next: 13.9. Writing an Inheritable Class"BORDER="0"></A></TD></TR></TABLE></DIV><DIVCLASS="sect1"><H2CLASS="sect1"><ACLASS="title"NAME="ch13-chap13_determining_0">13.8. Determining Subclass Membership</A></H2><DIVCLASS="sect2"><H3CLASS="sect2"><ACLASS="title"NAME="ch13-pgfId-1091">Problem<ACLASS="indexterm"NAME="ch13-idx-1000004549-0"></A><ACLASS="indexterm"NAME="ch13-idx-1000004549-1"></A><ACLASS="indexterm"NAME="ch13-idx-1000004549-2"></A><ACLASS="indexterm"NAME="ch13-idx-1000004549-3"></A></A></H3><PCLASS="para">You want to know whether an object is an instance of a particular class or that class's subclasses. Perhaps you want to decide whether a particular method can be called on an arbitrary object.</P></DIV><DIVCLASS="sect2"><H3CLASS="sect2"><ACLASS="title"NAME="ch13-pgfId-1097">Solution</A></H3><PCLASS="para">Use methods from the special UNIVERSAL class:</P><PRECLASS="programlisting">$obj->isa("HTTP::Message"); # as object methodHTTP::Response->isa("HTTP::Message"); # as class methodif ($obj->can("method_name")) { .... } # check method validity</PRE></DIV><DIVCLASS="sect2"><H3CLASS="sect2"><ACLASS="title"NAME="ch13-pgfId-1111">Discussion</A></H3><PCLASS="para">Wouldn't it be convenient if all objects were rooted at some ultimate base class? That way you could give every object common methods without having to add to each <CODECLASS="literal">@ISA</CODE>. Well, you can. You don't see it, but Perl pretends there's an extra element at the end of <CODECLASS="literal">@ISA </CODE> - the package named UNIVERSAL.</P><PCLASS="para">In version 5.003, no methods were predefined in UNIVERSAL, but you could put whatever you felt like into it. However, as of version 5.004, UNIVERSAL has a few methods in it already. These are built right into your Perl binary, so they don't take extra time to load. Predefined methods include <CODECLASS="literal">isa</CODE><ACLASS="indexterm"NAME="ch13-idx-1000004893-0"></A><ACLASS="indexterm"NAME="ch13-idx-1000004893-1"></A>, <CODECLASS="literal">can</CODE>, and <CODECLASS="literal">VERSION</CODE>. The <CODECLASS="literal">isa</CODE> method tells you whether an object or class "is" another one, without having to traverse the hierarchy yourself:</P><PRECLASS="programlisting">$has_io = $fd->isa("IO::Handle");$itza_handle = IO::Socket->isa("IO::Handle");</PRE><PCLASS="para">Arguably, it's usually best to try the method call. Explicit type checks like this are sometimes frowned upon as being too constraining.</P><PCLASS="para">The <CODECLASS="literal">can</CODE> method, called on behalf of that object or class, reports back whether its string argument is a callable method name in that class. In fact, it gives you back a function reference to that method:</P><PRECLASS="programlisting">$his_print_method = $obj->can('as_string');</PRE><PCLASS="para">Finally, the <CODECLASS="literal">VERSION</CODE> method checks whether the class (or the object's class) has a package global called <CODECLASS="literal">$VERSION</CODE> that's high enough, as in:</P><PRECLASS="programlisting">Some_Module->VERSION(3.0);$his_vers = $obj-><CODECLASS="literal">VERSION()</CODE>;</PRE><PCLASS="para">However, we don't usually call <CODECLASS="literal">VERSION</CODE> ourselves. Remember, in Perl an all-uppercase function name means that the function will be automatically called by Perl in some way. In this case, it happens when you say:</P><PRECLASS="programlisting">use Some_Module 3.0;</PRE><PCLASS="para">If you wanted to add version checking to your Person class explained above, add this to Person.pm:</P><PRECLASS="programlisting">use vars qw($VERSION);$VERSION = '1.01';<CODECLASS="literal"></CODE><ACLASS="indexterm"NAME="ch13-idx-1000004558-0"></A><ACLASS="indexterm"NAME="ch13-idx-1000004558-1"></A><ACLASS="indexterm"NAME="ch13-idx-1000004558-2"></A></PRE><PCLASS="para">Then, in the user code say <CODECLASS="literal">use</CODE> <CODECLASS="literal">Person</CODE> <CODECLASS="literal">1.01;</CODE> to make sure that you have at least that version number or higher available. This is not the same as loading in that exact version number; it just has to be at least that high. Lamentably, no support currently exists for concurrent installation of multiple versions of a module.<ACLASS="indexterm"NAME="ch13-idx-1000004552-0"></A><ACLASS="indexterm"NAME="ch13-idx-1000004552-1"></A><ACLASS="indexterm"NAME="ch13-idx-1000004552-2"></A><ACLASS="indexterm"NAME="ch13-idx-1000004552-3"></A></P></DIV><DIVCLASS="sect2"><H3CLASS="sect2"><ACLASS="title"NAME="ch13-pgfId-1147">See Also</A></H3><PCLASS="para">The documentation for the standard UNIVERSAL module; the <CODECLASS="literal">use</CODE> keyword in <ICLASS="filename">perlfunc </I>(1) and 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></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_08.htm"TITLE="13.7. Calling Methods Indirectly"><IMGSRC="../gifs/txtpreva.gif"ALT="Previous: 13.7. Calling Methods Indirectly"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_10.htm"TITLE="13.9. Writing an Inheritable Class"><IMGSRC="../gifs/txtnexta.gif"ALT="Next: 13.9. Writing an Inheritable Class"BORDER="0"></A></TD></TR><TR><TDALIGN="LEFT"VALIGN="TOP"WIDTH="228">13.7. Calling Methods Indirectly</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.9. Writing an Inheritable Class</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 + -