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.

527 lines
12 KiB

  1. =head1 NAME
  2. perlbot - Bag'o Object Tricks (the BOT)
  3. =head1 DESCRIPTION
  4. The following collection of tricks and hints is intended to whet curious
  5. appetites about such things as the use of instance variables and the
  6. mechanics of object and class relationships. The reader is encouraged to
  7. consult relevant textbooks for discussion of Object Oriented definitions and
  8. methodology. This is not intended as a tutorial for object-oriented
  9. programming or as a comprehensive guide to Perl's object oriented features,
  10. nor should it be construed as a style guide.
  11. The Perl motto still holds: There's more than one way to do it.
  12. =head1 OO SCALING TIPS
  13. =over 5
  14. =item 1
  15. Do not attempt to verify the type of $self. That'll break if the class is
  16. inherited, when the type of $self is valid but its package isn't what you
  17. expect. See rule 5.
  18. =item 2
  19. If an object-oriented (OO) or indirect-object (IO) syntax was used, then the
  20. object is probably the correct type and there's no need to become paranoid
  21. about it. Perl isn't a paranoid language anyway. If people subvert the OO
  22. or IO syntax then they probably know what they're doing and you should let
  23. them do it. See rule 1.
  24. =item 3
  25. Use the two-argument form of bless(). Let a subclass use your constructor.
  26. See L<INHERITING A CONSTRUCTOR>.
  27. =item 4
  28. The subclass is allowed to know things about its immediate superclass, the
  29. superclass is allowed to know nothing about a subclass.
  30. =item 5
  31. Don't be trigger happy with inheritance. A "using", "containing", or
  32. "delegation" relationship (some sort of aggregation, at least) is often more
  33. appropriate. See L<OBJECT RELATIONSHIPS>, L<USING RELATIONSHIP WITH SDBM>,
  34. and L<"DELEGATION">.
  35. =item 6
  36. The object is the namespace. Make package globals accessible via the
  37. object. This will remove the guess work about the symbol's home package.
  38. See L<CLASS CONTEXT AND THE OBJECT>.
  39. =item 7
  40. IO syntax is certainly less noisy, but it is also prone to ambiguities that
  41. can cause difficult-to-find bugs. Allow people to use the sure-thing OO
  42. syntax, even if you don't like it.
  43. =item 8
  44. Do not use function-call syntax on a method. You're going to be bitten
  45. someday. Someone might move that method into a superclass and your code
  46. will be broken. On top of that you're feeding the paranoia in rule 2.
  47. =item 9
  48. Don't assume you know the home package of a method. You're making it
  49. difficult for someone to override that method. See L<THINKING OF CODE REUSE>.
  50. =back
  51. =head1 INSTANCE VARIABLES
  52. An anonymous array or anonymous hash can be used to hold instance
  53. variables. Named parameters are also demonstrated.
  54. package Foo;
  55. sub new {
  56. my $type = shift;
  57. my %params = @_;
  58. my $self = {};
  59. $self->{'High'} = $params{'High'};
  60. $self->{'Low'} = $params{'Low'};
  61. bless $self, $type;
  62. }
  63. package Bar;
  64. sub new {
  65. my $type = shift;
  66. my %params = @_;
  67. my $self = [];
  68. $self->[0] = $params{'Left'};
  69. $self->[1] = $params{'Right'};
  70. bless $self, $type;
  71. }
  72. package main;
  73. $a = Foo->new( 'High' => 42, 'Low' => 11 );
  74. print "High=$a->{'High'}\n";
  75. print "Low=$a->{'Low'}\n";
  76. $b = Bar->new( 'Left' => 78, 'Right' => 40 );
  77. print "Left=$b->[0]\n";
  78. print "Right=$b->[1]\n";
  79. =head1 SCALAR INSTANCE VARIABLES
  80. An anonymous scalar can be used when only one instance variable is needed.
  81. package Foo;
  82. sub new {
  83. my $type = shift;
  84. my $self;
  85. $self = shift;
  86. bless \$self, $type;
  87. }
  88. package main;
  89. $a = Foo->new( 42 );
  90. print "a=$$a\n";
  91. =head1 INSTANCE VARIABLE INHERITANCE
  92. This example demonstrates how one might inherit instance variables from a
  93. superclass for inclusion in the new class. This requires calling the
  94. superclass's constructor and adding one's own instance variables to the new
  95. object.
  96. package Bar;
  97. sub new {
  98. my $type = shift;
  99. my $self = {};
  100. $self->{'buz'} = 42;
  101. bless $self, $type;
  102. }
  103. package Foo;
  104. @ISA = qw( Bar );
  105. sub new {
  106. my $type = shift;
  107. my $self = Bar->new;
  108. $self->{'biz'} = 11;
  109. bless $self, $type;
  110. }
  111. package main;
  112. $a = Foo->new;
  113. print "buz = ", $a->{'buz'}, "\n";
  114. print "biz = ", $a->{'biz'}, "\n";
  115. =head1 OBJECT RELATIONSHIPS
  116. The following demonstrates how one might implement "containing" and "using"
  117. relationships between objects.
  118. package Bar;
  119. sub new {
  120. my $type = shift;
  121. my $self = {};
  122. $self->{'buz'} = 42;
  123. bless $self, $type;
  124. }
  125. package Foo;
  126. sub new {
  127. my $type = shift;
  128. my $self = {};
  129. $self->{'Bar'} = Bar->new;
  130. $self->{'biz'} = 11;
  131. bless $self, $type;
  132. }
  133. package main;
  134. $a = Foo->new;
  135. print "buz = ", $a->{'Bar'}->{'buz'}, "\n";
  136. print "biz = ", $a->{'biz'}, "\n";
  137. =head1 OVERRIDING SUPERCLASS METHODS
  138. The following example demonstrates how to override a superclass method and
  139. then call the overridden method. The B<SUPER> pseudo-class allows the
  140. programmer to call an overridden superclass method without actually knowing
  141. where that method is defined.
  142. package Buz;
  143. sub goo { print "here's the goo\n" }
  144. package Bar; @ISA = qw( Buz );
  145. sub google { print "google here\n" }
  146. package Baz;
  147. sub mumble { print "mumbling\n" }
  148. package Foo;
  149. @ISA = qw( Bar Baz );
  150. sub new {
  151. my $type = shift;
  152. bless [], $type;
  153. }
  154. sub grr { print "grumble\n" }
  155. sub goo {
  156. my $self = shift;
  157. $self->SUPER::goo();
  158. }
  159. sub mumble {
  160. my $self = shift;
  161. $self->SUPER::mumble();
  162. }
  163. sub google {
  164. my $self = shift;
  165. $self->SUPER::google();
  166. }
  167. package main;
  168. $foo = Foo->new;
  169. $foo->mumble;
  170. $foo->grr;
  171. $foo->goo;
  172. $foo->google;
  173. =head1 USING RELATIONSHIP WITH SDBM
  174. This example demonstrates an interface for the SDBM class. This creates a
  175. "using" relationship between the SDBM class and the new class Mydbm.
  176. package Mydbm;
  177. require SDBM_File;
  178. require Tie::Hash;
  179. @ISA = qw( Tie::Hash );
  180. sub TIEHASH {
  181. my $type = shift;
  182. my $ref = SDBM_File->new(@_);
  183. bless {'dbm' => $ref}, $type;
  184. }
  185. sub FETCH {
  186. my $self = shift;
  187. my $ref = $self->{'dbm'};
  188. $ref->FETCH(@_);
  189. }
  190. sub STORE {
  191. my $self = shift;
  192. if (defined $_[0]){
  193. my $ref = $self->{'dbm'};
  194. $ref->STORE(@_);
  195. } else {
  196. die "Cannot STORE an undefined key in Mydbm\n";
  197. }
  198. }
  199. package main;
  200. use Fcntl qw( O_RDWR O_CREAT );
  201. tie %foo, "Mydbm", "Sdbm", O_RDWR|O_CREAT, 0640;
  202. $foo{'bar'} = 123;
  203. print "foo-bar = $foo{'bar'}\n";
  204. tie %bar, "Mydbm", "Sdbm2", O_RDWR|O_CREAT, 0640;
  205. $bar{'Cathy'} = 456;
  206. print "bar-Cathy = $bar{'Cathy'}\n";
  207. =head1 THINKING OF CODE REUSE
  208. One strength of Object-Oriented languages is the ease with which old code
  209. can use new code. The following examples will demonstrate first how one can
  210. hinder code reuse and then how one can promote code reuse.
  211. This first example illustrates a class which uses a fully-qualified method
  212. call to access the "private" method BAZ(). The second example will show
  213. that it is impossible to override the BAZ() method.
  214. package FOO;
  215. sub new {
  216. my $type = shift;
  217. bless {}, $type;
  218. }
  219. sub bar {
  220. my $self = shift;
  221. $self->FOO::private::BAZ;
  222. }
  223. package FOO::private;
  224. sub BAZ {
  225. print "in BAZ\n";
  226. }
  227. package main;
  228. $a = FOO->new;
  229. $a->bar;
  230. Now we try to override the BAZ() method. We would like FOO::bar() to call
  231. GOOP::BAZ(), but this cannot happen because FOO::bar() explicitly calls
  232. FOO::private::BAZ().
  233. package FOO;
  234. sub new {
  235. my $type = shift;
  236. bless {}, $type;
  237. }
  238. sub bar {
  239. my $self = shift;
  240. $self->FOO::private::BAZ;
  241. }
  242. package FOO::private;
  243. sub BAZ {
  244. print "in BAZ\n";
  245. }
  246. package GOOP;
  247. @ISA = qw( FOO );
  248. sub new {
  249. my $type = shift;
  250. bless {}, $type;
  251. }
  252. sub BAZ {
  253. print "in GOOP::BAZ\n";
  254. }
  255. package main;
  256. $a = GOOP->new;
  257. $a->bar;
  258. To create reusable code we must modify class FOO, flattening class
  259. FOO::private. The next example shows a reusable class FOO which allows the
  260. method GOOP::BAZ() to be used in place of FOO::BAZ().
  261. package FOO;
  262. sub new {
  263. my $type = shift;
  264. bless {}, $type;
  265. }
  266. sub bar {
  267. my $self = shift;
  268. $self->BAZ;
  269. }
  270. sub BAZ {
  271. print "in BAZ\n";
  272. }
  273. package GOOP;
  274. @ISA = qw( FOO );
  275. sub new {
  276. my $type = shift;
  277. bless {}, $type;
  278. }
  279. sub BAZ {
  280. print "in GOOP::BAZ\n";
  281. }
  282. package main;
  283. $a = GOOP->new;
  284. $a->bar;
  285. =head1 CLASS CONTEXT AND THE OBJECT
  286. Use the object to solve package and class context problems. Everything a
  287. method needs should be available via the object or should be passed as a
  288. parameter to the method.
  289. A class will sometimes have static or global data to be used by the
  290. methods. A subclass may want to override that data and replace it with new
  291. data. When this happens the superclass may not know how to find the new
  292. copy of the data.
  293. This problem can be solved by using the object to define the context of the
  294. method. Let the method look in the object for a reference to the data. The
  295. alternative is to force the method to go hunting for the data ("Is it in my
  296. class, or in a subclass? Which subclass?"), and this can be inconvenient
  297. and will lead to hackery. It is better just to let the object tell the
  298. method where that data is located.
  299. package Bar;
  300. %fizzle = ( 'Password' => 'XYZZY' );
  301. sub new {
  302. my $type = shift;
  303. my $self = {};
  304. $self->{'fizzle'} = \%fizzle;
  305. bless $self, $type;
  306. }
  307. sub enter {
  308. my $self = shift;
  309. # Don't try to guess if we should use %Bar::fizzle
  310. # or %Foo::fizzle. The object already knows which
  311. # we should use, so just ask it.
  312. #
  313. my $fizzle = $self->{'fizzle'};
  314. print "The word is ", $fizzle->{'Password'}, "\n";
  315. }
  316. package Foo;
  317. @ISA = qw( Bar );
  318. %fizzle = ( 'Password' => 'Rumple' );
  319. sub new {
  320. my $type = shift;
  321. my $self = Bar->new;
  322. $self->{'fizzle'} = \%fizzle;
  323. bless $self, $type;
  324. }
  325. package main;
  326. $a = Bar->new;
  327. $b = Foo->new;
  328. $a->enter;
  329. $b->enter;
  330. =head1 INHERITING A CONSTRUCTOR
  331. An inheritable constructor should use the second form of bless() which allows
  332. blessing directly into a specified class. Notice in this example that the
  333. object will be a BAR not a FOO, even though the constructor is in class FOO.
  334. package FOO;
  335. sub new {
  336. my $type = shift;
  337. my $self = {};
  338. bless $self, $type;
  339. }
  340. sub baz {
  341. print "in FOO::baz()\n";
  342. }
  343. package BAR;
  344. @ISA = qw(FOO);
  345. sub baz {
  346. print "in BAR::baz()\n";
  347. }
  348. package main;
  349. $a = BAR->new;
  350. $a->baz;
  351. =head1 DELEGATION
  352. Some classes, such as SDBM_File, cannot be effectively subclassed because
  353. they create foreign objects. Such a class can be extended with some sort of
  354. aggregation technique such as the "using" relationship mentioned earlier or
  355. by delegation.
  356. The following example demonstrates delegation using an AUTOLOAD() function to
  357. perform message-forwarding. This will allow the Mydbm object to behave
  358. exactly like an SDBM_File object. The Mydbm class could now extend the
  359. behavior by adding custom FETCH() and STORE() methods, if this is desired.
  360. package Mydbm;
  361. require SDBM_File;
  362. require Tie::Hash;
  363. @ISA = qw(Tie::Hash);
  364. sub TIEHASH {
  365. my $type = shift;
  366. my $ref = SDBM_File->new(@_);
  367. bless {'delegate' => $ref};
  368. }
  369. sub AUTOLOAD {
  370. my $self = shift;
  371. # The Perl interpreter places the name of the
  372. # message in a variable called $AUTOLOAD.
  373. # DESTROY messages should never be propagated.
  374. return if $AUTOLOAD =~ /::DESTROY$/;
  375. # Remove the package name.
  376. $AUTOLOAD =~ s/^Mydbm:://;
  377. # Pass the message to the delegate.
  378. $self->{'delegate'}->$AUTOLOAD(@_);
  379. }
  380. package main;
  381. use Fcntl qw( O_RDWR O_CREAT );
  382. tie %foo, "Mydbm", "adbm", O_RDWR|O_CREAT, 0640;
  383. $foo{'bar'} = 123;
  384. print "foo-bar = $foo{'bar'}\n";