📄 ch13_01.htm
字号:
CLASS="indexterm"NAME="ch13-idx-1000004441-0"></A><ACLASS="indexterm"NAME="ch13-idx-1000004441-1"></A> notation for method calls:</P><PRECLASS="programlisting">$lector = new Human::Cannibal;feed $lector "Zak";move $lector "New York";</PRE><PCLASS="para">is an alternative syntax for:</P><PRECLASS="programlisting">$lector = Human::Cannibal-><CODECLASS="literal">new();</CODE>$lector->feed("Zak");$lector->move("New York");</PRE><PCLASS="para">This indirect object notation is appealing to English speakers and familiar to C++ programmers (who use <CODECLASS="literal">new</CODE> this way). Do not be seduced. It has two grave problems. One is that it follows the same quirky rules as the filehandle slot in <CODECLASS="literal">print</CODE> and <CODECLASS="literal">printf</CODE>:</P><PRECLASS="programlisting">printf STDERR "stuff here\n";</PRE><PCLASS="para">This slot, if filled, must contain a bare symbol, a block, or a scalar variable name; it can't be any old scalar expression. This can lead to horribly confusing precedence problems, as in these next two lines:</P><PRECLASS="programlisting">move $obj->{FIELD}; # probably wrongmove $ary[$i]; # probably wrong</PRE><PCLASS="para">Surprisingly, those actually parse as:</P><PRECLASS="programlisting">$obj->move->{FIELD}; # Surprise!$ary->move->[$i]; # Surprise!</PRE><PCLASS="para">rather than as you might have expected:</P><PRECLASS="programlisting">$obj->{FIELD}-><CODECLASS="literal">move()</CODE>; # Nope, you wish$ary[$i]-><CODECLASS="literal">move;</CODE> # Nope, you wish</PRE><PCLASS="para">The second problem is that Perl must guess at compile time whether <CODECLASS="literal">name</CODE> and <CODECLASS="literal">move</CODE> are functions or methods. Usually Perl gets it right, but when it doesn't, you get a function call compiled as a method, or vice versa. This can introduce incredibly subtle bugs that are hard to unravel. The infix arrow notation using <CODECLASS="literal">-></CODE> doesn't suffer from either of these disturbing ambiguities, so we recommend you use it exclusively.<EMCLASS="emphasis"></EM><ACLASS="indexterm"NAME="ch13-idx-1000004443-0"></A><ACLASS="indexterm"NAME="ch13-idx-1000004443-1"></A></P></DIV><DIVCLASS="sect2"><H3CLASS="sect2"><ACLASS="title"NAME="ch13-chap13_some_0">Some Notes on Object Terminology</A></H3><PCLASS="para"><ACLASS="indexterm"NAME="ch13-idx-1000004451-0"></A>In the object-oriented world, many words describe only a few concepts. If you've programmed in another object-oriented language, you might like to know how familiar terms and concepts map onto Perl.</P><PCLASS="para">For example, it's common to call objects <EMCLASS="emphasis">instances</EM> of a class and those objects' methods <EMCLASS="emphasis">instance methods</EM><ACLASS="indexterm"NAME="ch13-idx-1000004457-0"></A>. Data fields peculiar to each object are often called <EMCLASS="emphasis">instance data</EM><ACLASS="indexterm"NAME="ch13-idx-1000004458-0"></A><ACLASS="indexterm"NAME="ch13-idx-1000004458-1"></A><ACLASS="indexterm"NAME="ch13-idx-1000004458-2"></A> or <EMCLASS="emphasis">object attributes</EM>, and data fields common to all members of that class are <EMCLASS="emphasis">class data</EM><ACLASS="indexterm"NAME="ch13-idx-1000004459-0"></A><ACLASS="indexterm"NAME="ch13-idx-1000004459-1"></A><ACLASS="indexterm"NAME="ch13-idx-1000004459-2"></A>, <EMCLASS="emphasis">class attributes</EM>, or <EMCLASS="emphasis">static data members</EM>.</P><PCLASS="para">Also, <EMCLASS="emphasis">base class</EM><ACLASS="indexterm"NAME="ch13-idx-1000004460-0"></A><ACLASS="indexterm"NAME="ch13-idx-1000004460-1"></A><ACLASS="indexterm"NAME="ch13-idx-1000004460-2"></A>, <EMCLASS="emphasis">generic class</EM>, and <EMCLASS="emphasis">superclass</EM> all describe the same notion (a parent or similar ancestor in the inheritance hierarchy), whereas <EMCLASS="emphasis">derived class</EM><ACLASS="indexterm"NAME="ch13-idx-1000004461-0"></A><ACLASS="indexterm"NAME="ch13-idx-1000004461-1"></A><ACLASS="indexterm"NAME="ch13-idx-1000004461-2"></A>, <EMCLASS="emphasis">specific class</EM>, and <EMCLASS="emphasis">subclass</EM> describe the opposite relationship (a child or descendent in the inheritance hierarchy).</P><PCLASS="para">C++ programmers have <EMCLASS="emphasis">static methods</EM>, <EMCLASS="emphasis">virtual methods</EM>, and <EMCLASS="emphasis">instance methods</EM>, but Perl only has <EMCLASS="emphasis">class methods</EM><ACLASS="indexterm"NAME="ch13-idx-1000004462-0"></A><ACLASS="indexterm"NAME="ch13-idx-1000004462-1"></A> and <EMCLASS="emphasis">object methods</EM>. Actually, Perl only has methods. Whether a method acts as a class or object method is determined solely by actual usage. You could call a class method (one expecting a string argument) on an object (one expecting a reference), or vice versa, but you shouldn't expect reasonable results if you do.</P><PCLASS="para">A C++ programmer thinks about global (class) constructors and destructors. These correspond to module initialization code and per-module <CODECLASS="literal">END{}</CODE> blocks respectively.</P><PCLASS="para">From the C++ perspective, all methods in Perl are virtual. This is why their arguments are never checked for function prototypes as regular built-in and user-defined functions can be. Prototypes are checked by the compiler at compile time. You can't determine until run time the function that a method has called.</P></DIV><DIVCLASS="sect2"><H3CLASS="sect2"><ACLASS="title"NAME="ch13-chap13_philosophical_0">Philosophical Aside</A></H3><PCLASS="para">In its OO programming, Perl gives you a lot of freedom: the ability to do things more than one way (you can bless any data type to make an object), to inspect and modify classes you didn't write (adding functions to their packages), and to use these to write tangled pits of misery - if that's really what you want to do.</P><PCLASS="para">Less flexible programming languages are usually more restrictive. Many are fanatically devoted to enforced privacy, compile-time type checking, complex function signatures, and a smorgasbord of other features. Perl doesn't provide these things with objects because it doesn't provide them anywhere else, either. Keep this in mind if you find Perl's object-oriented implementation weird. You only think it's weird because you're used to another language's philosophy. Perl's treatment of OO is perfectly sensible - if you think in Perl. For every problem that you can't solve by writing Perl as though it were Java or C++, there is a native Perl solution that works perfectly. The absolutely paranoid programmer can even have complete privacy: the <ICLASS="filename">perltoot </I>(1) manpage describes how to bless closures to produce objects that are as private as those in C++ (and more so).</P><PCLASS="para">Perl's objects are not wrong; they're differently right.</P></DIV><DIVCLASS="sect2"><H3CLASS="sect2"><ACLASS="title"NAME="ch13-chap13_suggested_0">See Also</A></H3><PCLASS="para">The general literature on object-oriented programming rarely refers directly to Perl. The documentation that came with Perl is a good place to begin learning about object-oriented programming, particularly the object tutorial <ICLASS="filename">perltoot </I>(1). For a reference, read <ICLASS="filename">perlobj </I>(1) and Chapter 5 of <ACLASS="citetitle"HREF="../prog/index.htm"TITLE="Programming Perl"><CITECLASS="citetitle">Programming Perl</CITE></A>. You might need it when you read <ICLASS="filename">perlbot</I> (1), which is full of object-oriented tricks.</P><PCLASS="para">Chapters <ACLASS="olink"HREF="../advprog/ch07_01.htm">7</A> and <ACLASS="olink"HREF="../advprog/ch08_01.htm">8</A> of <EMCLASS="emphasis">Advanced Perl Programming </EM>includes a discussion of object-oriented programming in Perl for those who have encountered objects before.<EMCLASS="emphasis"></EM><ACLASS="indexterm"NAME="ch13-idx-1000004453-0"></A></P></DIV></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="ch12_20.htm"TITLE="12.19. Program: Finding Versions and Descriptions of Installed Modules"><IMGSRC="../gifs/txtpreva.gif"ALT="Previous: 12.19. Program: Finding Versions and Descriptions of Installed Modules"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_02.htm"TITLE="13.1. Constructing an Object"><IMGSRC="../gifs/txtnexta.gif"ALT="Next: 13.1. Constructing an Object"BORDER="0"></A></TD></TR><TR><TDALIGN="LEFT"VALIGN="TOP"WIDTH="228">12.19. Program: Finding Versions and Descriptions of Installed Modules</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.1. Constructing an Object</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 + -