Leaked source code of windows server 2003
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

684 lines
31 KiB

  1. #Time-stamp: "2001-02-23 20:07:25 MST" -*-Text-*-
  2. # This document contains text in Perl "POD" format.
  3. # Use a POD viewer like perldoc or perlman to render it.
  4. =head1 NAME
  5. HTML::Tree::AboutObjects -- article: "User's View of Object-Oriented Modules"
  6. =head1 SYNOPSIS
  7. # This an article, not a module.
  8. =head1 DESCRIPTION
  9. The following article by Sean M. Burke first appeared in I<The Perl
  10. Journal> #17 and is copyright 2000 The Perl Journal. It appears
  11. courtesy of Jon Orwant and The Perl Journal. This document may be
  12. distributed under the same terms as Perl itself.
  13. =head1 A User's View of Object-Oriented Modules
  14. -- Sean M. Burke
  15. The first time that most Perl programmers run into object-oriented
  16. programming when they need to use a module whose interface is
  17. object-oriented. This is often a mystifying experience, since talk of
  18. "methods" and "constructors" is unintelligible to programmers who
  19. thought that functions and variables was all there was to worry about.
  20. Articles and books that explain object-oriented programming (OOP), do so
  21. in terms of how to program that way. That's understandable, and if you
  22. learn to write object-oriented code of your own, you'd find it easy to
  23. use object-oriented code that others write. But this approach is the
  24. I<long> way around for people whose immediate goal is just to use
  25. existing object-oriented modules, but who don't yet want to know all the
  26. gory details of having to write such modules for themselves.
  27. This article is for those programmers -- programmers who want to know
  28. about objects from the perspective of using object-oriented modules.
  29. =head2 Modules and Their Functional Interfaces
  30. Modules are the main way that Perl provides for bundling up code for
  31. later use by yourself or others. As I'm sure you can't help noticing
  32. from reading
  33. I<The Perl Journal>, CPAN (the Comprehensive Perl Archive
  34. Network) is the repository for modules (or groups of modules) that
  35. others have written, to do anything from composing music to accessing
  36. Web pages. A good deal of those modules even come with every
  37. installation of Perl.
  38. One module that you may have used before, and which is fairly typical in
  39. its interface, is Text::Wrap. It comes with Perl, so you don't even
  40. need to install it from CPAN. You use it in a program of yours, by
  41. having your program code say early on:
  42. use Text::Wrap;
  43. and after that, you can access a function called C<wrap>, which inserts
  44. line-breaks in text that you feed it, so that the text will be wrapped to
  45. seventy-two (or however many) columns.
  46. The way this C<use Text::Wrap> business works is that the module
  47. Text::Wrap exists as a file "Text/Wrap.pm" somewhere in one of your
  48. library directories. That file contains Perl code...
  49. =over
  50. Footnote: And mixed in with the Perl code, there's documentation, which
  51. is what you read with "perldoc Text::Wrap". The perldoc program simply
  52. ignores the code and formats the documentation text, whereas "use
  53. Text::Wrap" loads and runs the code while ignoring the documentation.
  54. =back
  55. ...which, among other things, defines a function called C<Text::Wrap::wrap>,
  56. and then C<exports> that function, which means that when you say C<wrap>
  57. after having said "use Text::Wrap", you'll be actually calling the
  58. C<Text::Wrap::wrap> function. Some modules don't export their
  59. functions, so you have to call them by their full name, like
  60. C<Text::Wrap::wrap(...parameters...)>.
  61. Regardless of whether the typical module exports the functions it
  62. provides, a module is basically just a container for chunks of code that
  63. do useful things. The way the module allows for you to interact with
  64. it, is its I<interface>. And when, like with Text::Wrap, its interface
  65. consists of functions, the module is said to have a B<functional
  66. interface>.
  67. =over
  68. Footnote: the term "function" (and therefore "functionI<al>") has
  69. various senses. I'm using the term here in its broadest sense, to
  70. refer to routines -- bits of code that are called by some name and
  71. which take parameters and return some value.
  72. =back
  73. Using modules with functional interfaces is straightforward -- instead
  74. of defining your own "wrap" function with C<sub wrap { ... }>, you
  75. entrust "use Text::Wrap" to do that for you, along with whatever other
  76. functions its defines and exports, according to the module's
  77. documentation. Without too much bother, you can even write your own
  78. modules to contain your frequently used functions; I suggest having a look at
  79. the C<perlmod> man page for more leads on doing this.
  80. =head2 Modules with Object-Oriented Interfaces
  81. So suppose that one day you want to write a program that will automate
  82. the process of C<ftp>ing a bunch of files from one server down to your
  83. local machine, and then off to another server.
  84. A quick browse through search.cpan.org turns up the module "Net::FTP",
  85. which you can download and install it using normal installation
  86. instructions (unless your sysadmin has already installed it, as many
  87. have).
  88. Like Text::Wrap or any other module with a familiarly functional
  89. interface, you start off using Net::FTP in your program by saying:
  90. use Net::FTP;
  91. However, that's where the similarity ends. The first hint of
  92. difference is that the documentation for Net::FTP refers to it as a
  93. B<class>. A class is a kind of module, but one that has an
  94. object-oriented interface.
  95. Whereas modules like Text::Wrap
  96. provide bits of useful code as I<functions>, to be called like
  97. C<function(...parameters...)> or like
  98. C<PackageName::function(...parameters...)>, Net::FTP and other modules
  99. with object-oriented interfaces provide B<methods>. Methods are sort of
  100. like functions in that they have a name and parameters; but methods
  101. look different, and are different, because you have to call them with a
  102. syntax that has a class name or an object as a special argument. I'll
  103. explain the syntax for method calls, and then later explain what they
  104. all mean.
  105. Some methods are meant to be called as B<class methods>, with the class
  106. name (same as the module name) as a special argument. Class methods
  107. look like this:
  108. ClassName->methodname(parameter1, parameter2, ...)
  109. ClassName->methodname() # if no parameters
  110. ClassName->methodname # same as above
  111. which you will sometimes see written:
  112. methodname ClassName (parameter1, parameter2, ...)
  113. methodname ClassName # if no parameters
  114. Basically all class methods are for making new objects, and methods that
  115. make objects are called "B<constructors>" (and the process of making them
  116. is called "constructing" or "instantiating"). Constructor methods
  117. typically have the name "new", or something including "new"
  118. ("new_from_file", etc.); but they can conceivably be named
  119. anything -- DBI's constructor method is named "connect", for example.
  120. The object that a constructor method returns is
  121. typically captured in a scalar variable:
  122. $object = ClassName->new(param1, param2...);
  123. Once you have an object (more later on exactly what that is), you can
  124. use the other kind of method call syntax, the syntax for B<object method>
  125. calls. Calling object methods is just like class methods, except
  126. that instead of the ClassName as the special argument,
  127. you use an expression that yeilds an "object". Usually this is
  128. just a scalar variable that you earlier captured the
  129. output of the constructor in. Object method calls look like this:
  130. $object->methodname(parameter1, parameter2, ...);
  131. $object->methodname() # if no parameters
  132. $object->methodname # same as above
  133. which is occasionally written as:
  134. methodname $object (parameter1, parameter2, ...)
  135. methodname $object # if no parameters
  136. Examples of method calls are:
  137. my $session1 = Net::FTP->new("ftp.myhost.com");
  138. # Calls a class method "new", from class Net::FTP,
  139. # with the single parameter "ftp.myhost.com",
  140. # and saves the return value (which is, as usual,
  141. # an object), in $session1.
  142. # Could also be written:
  143. # new Net::FTP('ftp.myhost.com')
  144. $session1->login("sburke","aoeuaoeu")
  145. || die "failed to login!\n";
  146. # calling the object method "login"
  147. print "Dir:\n", $session1->dir(), "\n";
  148. $session1->quit;
  149. # same as $session1->quit()
  150. print "Done\n";
  151. exit;
  152. Incidentally, I suggest always using the syntaxes with parentheses and
  153. "->" in them,
  154. =over
  155. Footnote: the character-pair "->" is supposed to look like an
  156. arrow, not "negative greater-than"!
  157. =back
  158. and avoiding the syntaxes that start out "methodname $object" or
  159. "methodname ModuleName". When everything's going right, they all mean
  160. the same thing as the "->" variants, but the syntax with "->" is more
  161. visually distinct from function calls, as well as being immune to some
  162. kinds of rare but puzzling ambiguities that can arise when you're trying
  163. to call methods that have the same name as subroutines you've defined.
  164. But, syntactic alternatives aside, all this talk of constructing objects
  165. and object methods begs the question -- what I<is> an object? There are
  166. several angles to this question that the rest of this article will
  167. answer in turn: what can you do with objects? what's in an object?
  168. what's an object value? and why do some modules use objects at all?
  169. =head2 What Can You Do with Objects?
  170. You've seen that you can make objects, and call object methods with
  171. them. But what are object methods for? The answer depends on the class:
  172. A Net::FTP object represents a session between your computer and an FTP
  173. server. So the methods you call on a Net::FTP object are for doing
  174. whatever you'd need to do across an FTP connection. You make the
  175. session and log in:
  176. my $session = Net::FTP->new('ftp.aol.com');
  177. die "Couldn't connect!" unless defined $session;
  178. # The class method call to "new" will return
  179. # the new object if it goes OK, otherwise it
  180. # will return undef.
  181. $session->login('sburke', 'p@ssw3rD')
  182. || die "Did I change my password again?";
  183. # The object method "login" will give a true
  184. # return value if actually logs in, otherwise
  185. # it'll return false.
  186. You can use the session object to change directory on that session:
  187. $session->cwd("/home/sburke/public_html")
  188. || die "Hey, that was REALLY supposed to work!";
  189. # if the cwd fails, it'll return false
  190. ...get files from the machine at the other end of the session...
  191. foreach my $f ('log_report_ua.txt', 'log_report_dom.txt',
  192. 'log_report_browsers.txt')
  193. {
  194. $session->get($f) || warn "Getting $f failed!"
  195. };
  196. ...and plenty else, ending finally with closing the connection:
  197. $session->quit();
  198. In short, object methods are for doing things related to (or with)
  199. whatever the object represents. For FTP sessions, it's about sending
  200. commands to the server at the other end of the connection, and that's
  201. about it -- there, methods are for doing something to the world outside
  202. the object, and the objects is just something that specifies what bit
  203. of the world (well, what FTP session) to act upon.
  204. With most other classes, however, the object itself stores some kind of
  205. information, and it typically makes no sense to do things with such an
  206. object without considering the data that's in the object.
  207. =head2 What's I<in> an Object?
  208. An object is (with rare exceptions) a data structure containing a
  209. bunch of attributes, each of which has a value, as well as a name
  210. that you use when you
  211. read or set the attribute's value. Some of the object's attributes are
  212. private, meaning you'll never see them documented because they're not
  213. for you to read or write; but most of the object's documented attributes
  214. are at least readable, and usually writeable, by you. Net::FTP objects
  215. are a bit thin on attributes, so we'll use objects from the class
  216. Business::US_Amort for this example. Business::US_Amort is a very
  217. simple class (available from CPAN) that I wrote for making calculations
  218. to do with loans (specifically, amortization, using US-style
  219. algorithms).
  220. An object of the class Business::US_Amort represents a loan with
  221. particular parameters, i.e., attributes. The most basic attributes of a
  222. "loan object" are its interest rate, its principal (how much money it's
  223. for), and it's term (how long it'll take to repay). You need to set
  224. these attributes before anything else can be done with the object. The
  225. way to get at those attributes for loan objects is just like the
  226. way to get at attributes for any class's objects: through accessors.
  227. An B<accessor> is simply any method that accesses (whether reading or
  228. writing, AKA getting or putting) some attribute in the given object.
  229. Moreover, accessors are the B<only> way that you can change
  230. an object's attributes. (If a module's documentation wants you to
  231. know about any other way, it'll tell you.)
  232. Usually, for simplicity's sake, an accessor is named after the attribute
  233. it reads or writes. With Business::US_Amort objects, the accessors you
  234. need to use first are C<principal>, C<interest_rate>, and C<term>.
  235. Then, with at least those attributes set, you can call the C<run> method
  236. to figure out several things about the loan. Then you can call various
  237. accessors, like C<total_paid_toward_interest>, to read the results:
  238. use Business::US_Amort;
  239. my $loan = Business::US_Amort->new;
  240. # Set the necessary attributes:
  241. $loan->principal(123654);
  242. $loan->interest_rate(9.25);
  243. $loan->term(20); # twenty years
  244. # NOW we know enough to calculate:
  245. $loan->run;
  246. # And see what came of that:
  247. print
  248. "Total paid toward interest: A WHOPPING ",
  249. $loan->total_paid_interest, "!!\n";
  250. This illustrates a convention that's common with accessors: calling the
  251. accessor with no arguments (as with $loan->total_paid_interest) usually
  252. means to read the value of that attribute, but providing a value (as
  253. with $loan->term(20)) means you want that attribute to be set to that
  254. value. This stands to reason: why would you be providing a value, if
  255. not to set the attribute to that value?
  256. Although a loan's term, principal, and interest rates are all single
  257. numeric values, an objects values can any kind of scalar, or an array,
  258. or even a hash. Moreover, an attribute's value(s) can be objects
  259. themselves. For example, consider MIDI files (as I wrote about in
  260. TPJ#13): a MIDI file usually consists of several tracks. A MIDI file is
  261. complex enough to merit being an object with attributes like its overall
  262. tempo, the file-format variant it's in, and the list of instrument
  263. tracks in the file. But tracks themselves are complex enough to be
  264. objects too, with attributes like their track-type, a list of MIDI
  265. commands if they're a MIDI track, or raw data if they're not. So I
  266. ended up writing the MIDI modules so that the "tracks" attribute of a
  267. MIDI::Opus object is an array of objects from the class MIDI::Track.
  268. This may seem like a runaround -- you ask what's in one object, and get
  269. I<another> object, or several! But in this case, it exactly reflects
  270. what the module is for -- MIDI files contain MIDI tracks, which then
  271. contain data.
  272. =head2 What is an Object Value?
  273. When you call a constructor like Net::FTP->new(I<hostname>), you get
  274. back an object value, a value you can later use, in combination with a
  275. method name, to call object methods.
  276. Now, so far we've been pretending, in the above examples, that the
  277. variables $session or $loan I<are> the objects you're dealing with.
  278. This idea is innocuous up to a point, but it's really a misconception
  279. that will, at best, limit you in what you know how to do. The reality
  280. is not that the variables $session or $query are objects; it's a little
  281. more indirect -- they I<hold> values that symbolize objects. The kind of
  282. value that $session or $query hold is what I'm calling an object value.
  283. To understand what kind of value this is, first think about the other
  284. kinds of scalar values you know about: The first two scalar values you
  285. probably ever ran into in Perl are B<numbers> and B<strings>, which you
  286. learned (or just assumed) will usually turn into each other on demand;
  287. that is, the three-character string "2.5" can become the quantity two
  288. and a half, and vice versa. Then, especially if you started using
  289. C<perl -w> early on, you learned about the B<undefined value>, which can
  290. turn into 0 if you treat it as a number, or the empty-string if you
  291. treat it as a string.
  292. =over
  293. Footnote: You may I<also> have been learning about references, in which
  294. case you're ready to hear that object values are just a kind of
  295. reference, except that they reflect the class that created thing they point
  296. to, instead of merely being a plain old array reference, hash reference,
  297. etc. I<If> this makes makes sense to you, and you want to know more
  298. about how objects are implemented in Perl, have a look at the
  299. C<perltoot> man page.
  300. =back
  301. And now you're learning about B<object values>. An object value is a
  302. value that points to a data structure somewhere in memory, which is
  303. where all the attributes for this object are stored. That data
  304. structure as a whole belongs to a class (probably the one you named in
  305. the constructor method, like ClassName->new), so that the object value
  306. can be used as part of object method calls.
  307. If you want to actually I<see> what an object value is, you might try
  308. just saying "print $object". That'll get you something like this:
  309. Net::FTP=GLOB(0x20154240)
  310. or
  311. Business::US_Amort=HASH(0x15424020)
  312. That's not very helpful if you wanted to really get at the object's
  313. insides, but that's because the object value is only a symbol for the
  314. object. This may all sound very abstruse and metaphysical, so a
  315. real-world allegory might be very helpful:
  316. =over
  317. You get an advertisement in the mail saying that you have been
  318. (im)personally selected to have the rare privilege of applying for a
  319. credit card. For whatever reason, I<this> offer sounds good to you, so you
  320. fill out the form and mail it back to the credit card company. They
  321. gleefully approve the application and create your account, and send you
  322. a card with a number on it.
  323. Now, you can do things with the number on that card -- clerks at stores
  324. can ring up things you want to buy, and charge your account by keying in
  325. the number on the card. You can pay for things you order online by
  326. punching in the card number as part of your online order. You can pay
  327. off part of the account by sending the credit card people some of your
  328. money (well, a check) with some note (usually the pre-printed slip)
  329. that has the card number for the account you want to pay toward. And you
  330. should be able to call the credit card company's computer and ask it
  331. things about the card, like its balance, its credit limit, its APR, and
  332. maybe an itemization of recent purchases ad payments.
  333. Now, what you're I<really> doing is manipulating a credit card
  334. I<account>, a completely abstract entity with some data attached to it
  335. (balance, APR, etc). But for ease of access, you have a credit card
  336. I<number> that is a symbol for that account. Now, that symbol is just a
  337. bunch of digits, and the number is effectively meaningless and useless
  338. in and of itself -- but in the appropriate context, it's understood to
  339. I<mean> the credit card account you're accessing.
  340. This is exactly the relationship between objects and object values, and
  341. from this analogy, several facts about object values are a bit more
  342. explicable:
  343. * An object value does nothing in and of itself, but it's useful when
  344. you use it in the context of an $object->method call, the same way that
  345. a card number is useful in the context of some operation dealing with a
  346. card account.
  347. Moreover, several copies of the same object value all refer to the same
  348. object, the same way that making several copies of your card number
  349. won't change the fact that they all still refer to the same single
  350. account (this is true whether you're "copying" the number by just
  351. writing it down on different slips of paper, or whether you go to the
  352. trouble of forging exact replicas of your own plastic credit card). That's
  353. why this:
  354. $x = Net::FTP->new("ftp.aol.com");
  355. $x->login("sburke", "aoeuaoeu");
  356. does the same thing as this:
  357. $x = Net::FTP->new("ftp.aol.com");
  358. $y = $x;
  359. $z = $y;
  360. $z->login("sburke", "aoeuaoeu");
  361. That is, $z and $y and $x are three different I<slots> for values,
  362. but what's in those slots are all object values pointing to the same
  363. object -- you don't have three different FTP connections, just three
  364. variables with values pointing to the some single FTP connection.
  365. * You can't tell much of anything about the object just by looking at
  366. the object value, any more than you can see your credit account balance
  367. by holding the plastic card up to the light, or by adding up the digits
  368. in your credit card number.
  369. * You can't just make up your own object values and have them work --
  370. they can come only from constructor methods of the appropriate class.
  371. Similarly, you get a credit card number I<only> by having a bank approve
  372. your application for a credit card account -- at which point I<they>
  373. let I<you> know what the number of your new card is.
  374. Now, there's even more to the fact that you can't just make up your own
  375. object value: even though you can print an object value and get a string
  376. like "Net::FTP=GLOB(0x20154240)", that string is just a
  377. I<representation> of an object value.
  378. Internally, an object value has a basically different type from a
  379. string, or a number, or the undefined value -- if $x holds a real
  380. string, then that value's slot in memory says "this is a value of type
  381. I<string>, and its characters are...", whereas if it's an object value,
  382. the value's slot in memory says, "this is a value of type I<reference>,
  383. and the location in memory that it points to is..." (and by looking at
  384. what's at that location, Perl can tell the class of what's there).
  385. Perl programmers typically don't have to think about all these details
  386. of Perl's internals. Many other languages force you to be more
  387. conscious of the differences between all of these (and also between
  388. types of numbers, which are stored differently depending on their size
  389. and whether they have fractional parts). But Perl does its best to
  390. hide the different types of scalars from you -- it turns numbers into
  391. strings and back as needed, and takes the string or number
  392. representation of undef or of object values as needed. However, you
  393. can't go from a string representation of an object value, back to an
  394. object value. And that's why this doesn't work:
  395. $x = Net::FTP->new('ftp.aol.com');
  396. $y = Net::FTP->new('ftp.netcom.com');
  397. $z = Net::FTP->new('ftp.qualcomm.com');
  398. $all = join(' ', $x,$y,$z); # !!!
  399. ...later...
  400. ($aol, $netcom, $qualcomm) = split(' ', $all); # !!!
  401. $aol->login("sburke", "aoeuaoeu");
  402. $netcom->login("sburke", "qjkxqjkx");
  403. $qualcomm->login("smb", "dhtndhtn");
  404. This fails because $aol ends up holding merely the B<string representation>
  405. of the object value from $x, not the object value itself -- when
  406. C<join> tried to join the characters of the "strings" $x, $y, and $z,
  407. Perl saw that they weren't strings at all, so it gave C<join> their
  408. string representations.
  409. Unfortunately, this distinction between object values and their string
  410. representations doesn't really fit into the analogy of credit card
  411. numbers, because credit card numbers really I<are> numbers -- even
  412. thought they don't express any meaningful quantity, if you stored them
  413. in a database as a quantity (as opposed to just an ASCII string),
  414. that wouldn't stop them from being valid as credit card numbers.
  415. This may seem rather academic, but there's there's two common mistakes
  416. programmers new to objects often make, which make sense only in terms of
  417. the distinction between object values and their string representations:
  418. The first common error involves forgetting (or never having known in the
  419. first place) that when you go to use a value as a hash key, Perl uses
  420. the string representation of that value. When you want to use the
  421. numeric value two and a half as a key, Perl turns it into the
  422. three-character string "2.5". But if you then want to use that string
  423. as a number, Perl will treat it as meaning two and a half, so you're
  424. usually none the wiser that Perl converted the number to a string and
  425. back. But recall that Perl can't turn strings back into objects -- so
  426. if you tried to use a Net::FTP object value as a hash key, Perl actually
  427. used its string representation, like "Net::FTP=GLOB(0x20154240)", but
  428. that string is unusable as an object value. (Incidentally, there's
  429. a module Tie::RefHash that implements hashes that I<do> let you use
  430. real object-values as keys.)
  431. The second common error with object values is in
  432. trying to save an object value to disk (whether printing it to a
  433. file, or storing it in a conventional database file). All you'll get is the
  434. string, which will be useless.
  435. When you want to save an object and restore it later, you may find that
  436. the object's class already provides a method specifically for this. For
  437. example, MIDI::Opus provides methods for writing an object to disk as a
  438. standard MIDI file. The file can later be read back into memory by
  439. a MIDI::Opus constructor method, which will return a new MIDI::Opus
  440. object representing whatever file you tell it to read into memory.
  441. Similar methods are available with, for example, classes that
  442. manipulate graphic images and can save them to files, which can be read
  443. back later.
  444. But some classes, like Business::US_Amort, provide no such methods for
  445. storing an object in a file. When this is the case, you can try
  446. using any of the Data::Dumper, Storable, or FreezeThaw modules. Using
  447. these will be unproblematic for objects of most classes, but it may run
  448. into limitations with others. For example, a Business::US_Amort
  449. object can be turned into a string with Data::Dumper, and that string
  450. written to a file. When it's restored later, its attributes will be
  451. accessable as normal. But in the unlikely case that the loan object was
  452. saved in mid-calculation, the calculation may not be resumable. This is
  453. because of the way that that I<particular> class does its calculations,
  454. but similar limitations may occur with objects from other classses.
  455. But often, even I<wanting> to save an object is basically wrong -- what would
  456. saving an ftp I<session> even mean? Saving the hostname, username, and
  457. password? current directory on both machines? the local TCP/IP port
  458. number? In the case of "saving" a Net::FTP object, you're better off
  459. just saving whatever details you actually need for your own purposes,
  460. so that you can make a new object later and just set those values for it.
  461. =head2 So Why Do Some Modules Use Objects?
  462. All these details of using objects are definitely enough to make you
  463. wonder -- is it worth the bother? If you're a module author, writing
  464. your module with an object-oriented interface restricts the audience of
  465. potential users to those who understand the basic concepts of objects
  466. and object values, as well as Perl's syntax for calling methods. Why
  467. complicate things by having an object-oriented interface?
  468. A somewhat esoteric answer is that a module has an object-oriented
  469. interface because the module's insides are written in an
  470. object-oriented style. This article is about the basics of
  471. object-oriented I<interfaces>, and it'd be going far afield to explain
  472. what object-oriented I<design> is. But the short story is that
  473. object-oriented design is just one way of attacking messy problems.
  474. It's a way that many programmers find very helpful (and which others
  475. happen to find to be far more of a hassle than it's worth,
  476. incidentally), and it just happens to show up for you, the module user,
  477. as merely the style of interface.
  478. But a simpler answer is that a functional interface is sometimes a
  479. hindrance, because it limits the number of things you can do at once --
  480. limiting it, in fact, to one. For many problems that some modules are
  481. meant to solve, doing without an object-oriented interface would be like
  482. wishing that Perl didn't use filehandles. The ideas are rather simpler
  483. -- just imagine that Perl let you access files, but I<only> one at a
  484. time, with code like:
  485. open("foo.txt") || die "Can't open foo.txt: $!";
  486. while(readline) {
  487. print $_ if /bar/;
  488. }
  489. close;
  490. That hypothetical kind of Perl would be simpler, by doing without
  491. filehandles. But you'd be out of luck if you wanted to read from
  492. one file while reading from another, or read from two and print to a
  493. third.
  494. In the same way, a functional FTP module would be fine for just
  495. uploading files to one server at a time, but it wouldn't allow you to
  496. easily write programs that make need to use I<several> simultaneous
  497. sessions (like "look at server A and server B, and if A has a file
  498. called X.dat, then download it locally and then upload it to server B --
  499. except if B has a file called Y.dat, in which case do it the other way
  500. around").
  501. Some kinds of problems that modules solve just lend themselves to an
  502. object-oriented interface. For those kinds of tasks, a functional
  503. interface would be more familiar, but less powerful. Learning to use
  504. object-oriented modules' interfaces does require becoming comfortable
  505. with the concepts from this article. But in the end it will allow you
  506. to use a broader range of modules and, with them, to write programs
  507. that can do more.
  508. B<[end body of article]>
  509. =head2 [Author Credit]
  510. Sean M. Burke has contributed several modules to CPAN, about half of
  511. them object-oriented.
  512. [The next section should be in a greybox:]
  513. =head2 The Gory Details
  514. For sake of clarity of explanation, I had to oversimplify some of the
  515. facts about objects. Here's a few of the gorier details:
  516. * Every example I gave of a constructor was a class method. But object
  517. methods can be constructors, too, if the class was written to work that
  518. way: $new = $old->copy, $node_y = $node_x->new_subnode, or the like.
  519. * I've given the impression that there's two kinds of methods: object
  520. methods and class methods. In fact, the same method can be both,
  521. because it's not the kind of method it is, but the kind of calls it's
  522. written to accept -- calls that pass an object, or calls that pass a
  523. class-name.
  524. * The term "object value" isn't something you'll find used much anywhere
  525. else. It's just my shorthand for what would properly be called an
  526. "object reference" or "reference to a blessed item". In fact, people
  527. usually say "object" when they properly mean a reference to that object.
  528. * I mentioned creating objects with I<con>structors, but I didn't
  529. mention destroying them with I<de>structor -- a destructor is a kind of
  530. method that you call to tidy up the object once you're done with it, and
  531. want it to neatly go away (close connections, delete temporary files,
  532. free up memory, etc). But because of the way Perl handles memory,
  533. most modules won't require the user to know about destructors.
  534. * I said that class method syntax has to have the class name, as in
  535. $session = B<Net::FTP>->new($host). Actually, you can instead use any
  536. expression that returns a class name: $ftp_class = 'Net::FTP'; $session
  537. = B<$ftp_class>->new($host). Moreover, instead of the method name for
  538. object- or class-method calls, you can use a scalar holding the method
  539. name: $foo->B<$method>($host). But, in practice, these syntaxes are
  540. rarely useful.
  541. And finally, to learn about objects from the perspective of writing
  542. your own classes, see the C<perltoot> documentation,
  543. or Damian Conway's exhaustive and clear book I<Object Oriented Perl>
  544. (Manning Publications 1999, ISBN 1-884777-79-1).
  545. =head1 BACK
  546. Return to the L<HTML::Tree|HTML::Tree> docs.
  547. =cut