| 1 | .\" Automatically generated by Pod::Man v1.37, Pod::Parser v1.32 |
| 2 | .\" |
| 3 | .\" Standard preamble: |
| 4 | .\" ======================================================================== |
| 5 | .de Sh \" Subsection heading |
| 6 | .br |
| 7 | .if t .Sp |
| 8 | .ne 5 |
| 9 | .PP |
| 10 | \fB\\$1\fR |
| 11 | .PP |
| 12 | .. |
| 13 | .de Sp \" Vertical space (when we can't use .PP) |
| 14 | .if t .sp .5v |
| 15 | .if n .sp |
| 16 | .. |
| 17 | .de Vb \" Begin verbatim text |
| 18 | .ft CW |
| 19 | .nf |
| 20 | .ne \\$1 |
| 21 | .. |
| 22 | .de Ve \" End verbatim text |
| 23 | .ft R |
| 24 | .fi |
| 25 | .. |
| 26 | .\" Set up some character translations and predefined strings. \*(-- will |
| 27 | .\" give an unbreakable dash, \*(PI will give pi, \*(L" will give a left |
| 28 | .\" double quote, and \*(R" will give a right double quote. | will give a |
| 29 | .\" real vertical bar. \*(C+ will give a nicer C++. Capital omega is used to |
| 30 | .\" do unbreakable dashes and therefore won't be available. \*(C` and \*(C' |
| 31 | .\" expand to `' in nroff, nothing in troff, for use with C<>. |
| 32 | .tr \(*W-|\(bv\*(Tr |
| 33 | .ds C+ C\v'-.1v'\h'-1p'\s-2+\h'-1p'+\s0\v'.1v'\h'-1p' |
| 34 | .ie n \{\ |
| 35 | . ds -- \(*W- |
| 36 | . ds PI pi |
| 37 | . if (\n(.H=4u)&(1m=24u) .ds -- \(*W\h'-12u'\(*W\h'-12u'-\" diablo 10 pitch |
| 38 | . if (\n(.H=4u)&(1m=20u) .ds -- \(*W\h'-12u'\(*W\h'-8u'-\" diablo 12 pitch |
| 39 | . ds L" "" |
| 40 | . ds R" "" |
| 41 | . ds C` "" |
| 42 | . ds C' "" |
| 43 | 'br\} |
| 44 | .el\{\ |
| 45 | . ds -- \|\(em\| |
| 46 | . ds PI \(*p |
| 47 | . ds L" `` |
| 48 | . ds R" '' |
| 49 | 'br\} |
| 50 | .\" |
| 51 | .\" If the F register is turned on, we'll generate index entries on stderr for |
| 52 | .\" titles (.TH), headers (.SH), subsections (.Sh), items (.Ip), and index |
| 53 | .\" entries marked with X<> in POD. Of course, you'll have to process the |
| 54 | .\" output yourself in some meaningful fashion. |
| 55 | .if \nF \{\ |
| 56 | . de IX |
| 57 | . tm Index:\\$1\t\\n%\t"\\$2" |
| 58 | .. |
| 59 | . nr % 0 |
| 60 | . rr F |
| 61 | .\} |
| 62 | .\" |
| 63 | .\" For nroff, turn off justification. Always turn off hyphenation; it makes |
| 64 | .\" way too many mistakes in technical documents. |
| 65 | .hy 0 |
| 66 | .if n .na |
| 67 | .\" |
| 68 | .\" Accent mark definitions (@(#)ms.acc 1.5 88/02/08 SMI; from UCB 4.2). |
| 69 | .\" Fear. Run. Save yourself. No user-serviceable parts. |
| 70 | . \" fudge factors for nroff and troff |
| 71 | .if n \{\ |
| 72 | . ds #H 0 |
| 73 | . ds #V .8m |
| 74 | . ds #F .3m |
| 75 | . ds #[ \f1 |
| 76 | . ds #] \fP |
| 77 | .\} |
| 78 | .if t \{\ |
| 79 | . ds #H ((1u-(\\\\n(.fu%2u))*.13m) |
| 80 | . ds #V .6m |
| 81 | . ds #F 0 |
| 82 | . ds #[ \& |
| 83 | . ds #] \& |
| 84 | .\} |
| 85 | . \" simple accents for nroff and troff |
| 86 | .if n \{\ |
| 87 | . ds ' \& |
| 88 | . ds ` \& |
| 89 | . ds ^ \& |
| 90 | . ds , \& |
| 91 | . ds ~ ~ |
| 92 | . ds / |
| 93 | .\} |
| 94 | .if t \{\ |
| 95 | . ds ' \\k:\h'-(\\n(.wu*8/10-\*(#H)'\'\h"|\\n:u" |
| 96 | . ds ` \\k:\h'-(\\n(.wu*8/10-\*(#H)'\`\h'|\\n:u' |
| 97 | . ds ^ \\k:\h'-(\\n(.wu*10/11-\*(#H)'^\h'|\\n:u' |
| 98 | . ds , \\k:\h'-(\\n(.wu*8/10)',\h'|\\n:u' |
| 99 | . ds ~ \\k:\h'-(\\n(.wu-\*(#H-.1m)'~\h'|\\n:u' |
| 100 | . ds / \\k:\h'-(\\n(.wu*8/10-\*(#H)'\z\(sl\h'|\\n:u' |
| 101 | .\} |
| 102 | . \" troff and (daisy-wheel) nroff accents |
| 103 | .ds : \\k:\h'-(\\n(.wu*8/10-\*(#H+.1m+\*(#F)'\v'-\*(#V'\z.\h'.2m+\*(#F'.\h'|\\n:u'\v'\*(#V' |
| 104 | .ds 8 \h'\*(#H'\(*b\h'-\*(#H' |
| 105 | .ds o \\k:\h'-(\\n(.wu+\w'\(de'u-\*(#H)/2u'\v'-.3n'\*(#[\z\(de\v'.3n'\h'|\\n:u'\*(#] |
| 106 | .ds d- \h'\*(#H'\(pd\h'-\w'~'u'\v'-.25m'\f2\(hy\fP\v'.25m'\h'-\*(#H' |
| 107 | .ds D- D\\k:\h'-\w'D'u'\v'-.11m'\z\(hy\v'.11m'\h'|\\n:u' |
| 108 | .ds th \*(#[\v'.3m'\s+1I\s-1\v'-.3m'\h'-(\w'I'u*2/3)'\s-1o\s+1\*(#] |
| 109 | .ds Th \*(#[\s+2I\s-2\h'-\w'I'u*3/5'\v'-.3m'o\v'.3m'\*(#] |
| 110 | .ds ae a\h'-(\w'a'u*4/10)'e |
| 111 | .ds Ae A\h'-(\w'A'u*4/10)'E |
| 112 | . \" corrections for vroff |
| 113 | .if v .ds ~ \\k:\h'-(\\n(.wu*9/10-\*(#H)'\s-2\u~\d\s+2\h'|\\n:u' |
| 114 | .if v .ds ^ \\k:\h'-(\\n(.wu*10/11-\*(#H)'\v'-.4m'^\v'.4m'\h'|\\n:u' |
| 115 | . \" for low resolution devices (crt and lpr) |
| 116 | .if \n(.H>23 .if \n(.V>19 \ |
| 117 | \{\ |
| 118 | . ds : e |
| 119 | . ds 8 ss |
| 120 | . ds o a |
| 121 | . ds d- d\h'-1'\(ga |
| 122 | . ds D- D\h'-1'\(hy |
| 123 | . ds th \o'bp' |
| 124 | . ds Th \o'LP' |
| 125 | . ds ae ae |
| 126 | . ds Ae AE |
| 127 | .\} |
| 128 | .rm #[ #] #H #V #F C |
| 129 | .\" ======================================================================== |
| 130 | .\" |
| 131 | .IX Title "PERLBOT 1" |
| 132 | .TH PERLBOT 1 "2006-01-07" "perl v5.8.8" "Perl Programmers Reference Guide" |
| 133 | .SH "NAME" |
| 134 | perlbot \- Bag'o Object Tricks (the BOT) |
| 135 | .SH "DESCRIPTION" |
| 136 | .IX Header "DESCRIPTION" |
| 137 | The following collection of tricks and hints is intended to whet curious |
| 138 | appetites about such things as the use of instance variables and the |
| 139 | mechanics of object and class relationships. The reader is encouraged to |
| 140 | consult relevant textbooks for discussion of Object Oriented definitions and |
| 141 | methodology. This is not intended as a tutorial for object-oriented |
| 142 | programming or as a comprehensive guide to Perl's object oriented features, |
| 143 | nor should it be construed as a style guide. If you're looking for tutorials, |
| 144 | be sure to read perlboot, perltoot, and perltooc. |
| 145 | .PP |
| 146 | The Perl motto still holds: There's more than one way to do it. |
| 147 | .SH "OO SCALING TIPS" |
| 148 | .IX Header "OO SCALING TIPS" |
| 149 | .IP "1" 5 |
| 150 | .IX Item "1" |
| 151 | Do not attempt to verify the type of \f(CW$self\fR. That'll break if the class is |
| 152 | inherited, when the type of \f(CW$self\fR is valid but its package isn't what you |
| 153 | expect. See rule 5. |
| 154 | .IP "2" 5 |
| 155 | .IX Item "2" |
| 156 | If an object-oriented (\s-1OO\s0) or indirect-object (\s-1IO\s0) syntax was used, then the |
| 157 | object is probably the correct type and there's no need to become paranoid |
| 158 | about it. Perl isn't a paranoid language anyway. If people subvert the \s-1OO\s0 |
| 159 | or \s-1IO\s0 syntax then they probably know what they're doing and you should let |
| 160 | them do it. See rule 1. |
| 161 | .IP "3" 5 |
| 162 | .IX Item "3" |
| 163 | Use the two-argument form of \fIbless()\fR. Let a subclass use your constructor. |
| 164 | See \*(L"\s-1INHERITING\s0 A \s-1CONSTRUCTOR\s0\*(R". |
| 165 | .IP "4" 5 |
| 166 | .IX Item "4" |
| 167 | The subclass is allowed to know things about its immediate superclass, the |
| 168 | superclass is allowed to know nothing about a subclass. |
| 169 | .IP "5" 5 |
| 170 | .IX Item "5" |
| 171 | Don't be trigger happy with inheritance. A \*(L"using\*(R", \*(L"containing\*(R", or |
| 172 | \&\*(L"delegation\*(R" relationship (some sort of aggregation, at least) is often more |
| 173 | appropriate. See \*(L"\s-1OBJECT\s0 \s-1RELATIONSHIPS\s0\*(R", \*(L"\s-1USING\s0 \s-1RELATIONSHIP\s0 \s-1WITH\s0 \s-1SDBM\s0\*(R", |
| 174 | and \*(L"\s-1DELEGATION\s0\*(R". |
| 175 | .IP "6" 5 |
| 176 | .IX Item "6" |
| 177 | The object is the namespace. Make package globals accessible via the |
| 178 | object. This will remove the guess work about the symbol's home package. |
| 179 | See \*(L"\s-1CLASS\s0 \s-1CONTEXT\s0 \s-1AND\s0 \s-1THE\s0 \s-1OBJECT\s0\*(R". |
| 180 | .IP "7" 5 |
| 181 | .IX Item "7" |
| 182 | \&\s-1IO\s0 syntax is certainly less noisy, but it is also prone to ambiguities that |
| 183 | can cause difficult-to-find bugs. Allow people to use the sure-thing \s-1OO\s0 |
| 184 | syntax, even if you don't like it. |
| 185 | .IP "8" 5 |
| 186 | .IX Item "8" |
| 187 | Do not use function-call syntax on a method. You're going to be bitten |
| 188 | someday. Someone might move that method into a superclass and your code |
| 189 | will be broken. On top of that you're feeding the paranoia in rule 2. |
| 190 | .IP "9" 5 |
| 191 | .IX Item "9" |
| 192 | Don't assume you know the home package of a method. You're making it |
| 193 | difficult for someone to override that method. See \*(L"\s-1THINKING\s0 \s-1OF\s0 \s-1CODE\s0 \s-1REUSE\s0\*(R". |
| 194 | .SH "INSTANCE VARIABLES" |
| 195 | .IX Header "INSTANCE VARIABLES" |
| 196 | An anonymous array or anonymous hash can be used to hold instance |
| 197 | variables. Named parameters are also demonstrated. |
| 198 | .PP |
| 199 | .Vb 1 |
| 200 | \& package Foo; |
| 201 | .Ve |
| 202 | .PP |
| 203 | .Vb 8 |
| 204 | \& sub new { |
| 205 | \& my $type = shift; |
| 206 | \& my %params = @_; |
| 207 | \& my $self = {}; |
| 208 | \& $self->{'High'} = $params{'High'}; |
| 209 | \& $self->{'Low'} = $params{'Low'}; |
| 210 | \& bless $self, $type; |
| 211 | \& } |
| 212 | .Ve |
| 213 | .PP |
| 214 | .Vb 1 |
| 215 | \& package Bar; |
| 216 | .Ve |
| 217 | .PP |
| 218 | .Vb 8 |
| 219 | \& sub new { |
| 220 | \& my $type = shift; |
| 221 | \& my %params = @_; |
| 222 | \& my $self = []; |
| 223 | \& $self->[0] = $params{'Left'}; |
| 224 | \& $self->[1] = $params{'Right'}; |
| 225 | \& bless $self, $type; |
| 226 | \& } |
| 227 | .Ve |
| 228 | .PP |
| 229 | .Vb 1 |
| 230 | \& package main; |
| 231 | .Ve |
| 232 | .PP |
| 233 | .Vb 3 |
| 234 | \& $a = Foo->new( 'High' => 42, 'Low' => 11 ); |
| 235 | \& print "High=$a->{'High'}\en"; |
| 236 | \& print "Low=$a->{'Low'}\en"; |
| 237 | .Ve |
| 238 | .PP |
| 239 | .Vb 3 |
| 240 | \& $b = Bar->new( 'Left' => 78, 'Right' => 40 ); |
| 241 | \& print "Left=$b->[0]\en"; |
| 242 | \& print "Right=$b->[1]\en"; |
| 243 | .Ve |
| 244 | .SH "SCALAR INSTANCE VARIABLES" |
| 245 | .IX Header "SCALAR INSTANCE VARIABLES" |
| 246 | An anonymous scalar can be used when only one instance variable is needed. |
| 247 | .PP |
| 248 | .Vb 1 |
| 249 | \& package Foo; |
| 250 | .Ve |
| 251 | .PP |
| 252 | .Vb 6 |
| 253 | \& sub new { |
| 254 | \& my $type = shift; |
| 255 | \& my $self; |
| 256 | \& $self = shift; |
| 257 | \& bless \e$self, $type; |
| 258 | \& } |
| 259 | .Ve |
| 260 | .PP |
| 261 | .Vb 1 |
| 262 | \& package main; |
| 263 | .Ve |
| 264 | .PP |
| 265 | .Vb 2 |
| 266 | \& $a = Foo->new( 42 ); |
| 267 | \& print "a=$$a\en"; |
| 268 | .Ve |
| 269 | .SH "INSTANCE VARIABLE INHERITANCE" |
| 270 | .IX Header "INSTANCE VARIABLE INHERITANCE" |
| 271 | This example demonstrates how one might inherit instance variables from a |
| 272 | superclass for inclusion in the new class. This requires calling the |
| 273 | superclass's constructor and adding one's own instance variables to the new |
| 274 | object. |
| 275 | .PP |
| 276 | .Vb 1 |
| 277 | \& package Bar; |
| 278 | .Ve |
| 279 | .PP |
| 280 | .Vb 6 |
| 281 | \& sub new { |
| 282 | \& my $type = shift; |
| 283 | \& my $self = {}; |
| 284 | \& $self->{'buz'} = 42; |
| 285 | \& bless $self, $type; |
| 286 | \& } |
| 287 | .Ve |
| 288 | .PP |
| 289 | .Vb 2 |
| 290 | \& package Foo; |
| 291 | \& @ISA = qw( Bar ); |
| 292 | .Ve |
| 293 | .PP |
| 294 | .Vb 6 |
| 295 | \& sub new { |
| 296 | \& my $type = shift; |
| 297 | \& my $self = Bar->new; |
| 298 | \& $self->{'biz'} = 11; |
| 299 | \& bless $self, $type; |
| 300 | \& } |
| 301 | .Ve |
| 302 | .PP |
| 303 | .Vb 1 |
| 304 | \& package main; |
| 305 | .Ve |
| 306 | .PP |
| 307 | .Vb 3 |
| 308 | \& $a = Foo->new; |
| 309 | \& print "buz = ", $a->{'buz'}, "\en"; |
| 310 | \& print "biz = ", $a->{'biz'}, "\en"; |
| 311 | .Ve |
| 312 | .SH "OBJECT RELATIONSHIPS" |
| 313 | .IX Header "OBJECT RELATIONSHIPS" |
| 314 | The following demonstrates how one might implement \*(L"containing\*(R" and \*(L"using\*(R" |
| 315 | relationships between objects. |
| 316 | .PP |
| 317 | .Vb 1 |
| 318 | \& package Bar; |
| 319 | .Ve |
| 320 | .PP |
| 321 | .Vb 6 |
| 322 | \& sub new { |
| 323 | \& my $type = shift; |
| 324 | \& my $self = {}; |
| 325 | \& $self->{'buz'} = 42; |
| 326 | \& bless $self, $type; |
| 327 | \& } |
| 328 | .Ve |
| 329 | .PP |
| 330 | .Vb 1 |
| 331 | \& package Foo; |
| 332 | .Ve |
| 333 | .PP |
| 334 | .Vb 7 |
| 335 | \& sub new { |
| 336 | \& my $type = shift; |
| 337 | \& my $self = {}; |
| 338 | \& $self->{'Bar'} = Bar->new; |
| 339 | \& $self->{'biz'} = 11; |
| 340 | \& bless $self, $type; |
| 341 | \& } |
| 342 | .Ve |
| 343 | .PP |
| 344 | .Vb 1 |
| 345 | \& package main; |
| 346 | .Ve |
| 347 | .PP |
| 348 | .Vb 3 |
| 349 | \& $a = Foo->new; |
| 350 | \& print "buz = ", $a->{'Bar'}->{'buz'}, "\en"; |
| 351 | \& print "biz = ", $a->{'biz'}, "\en"; |
| 352 | .Ve |
| 353 | .SH "OVERRIDING SUPERCLASS METHODS" |
| 354 | .IX Header "OVERRIDING SUPERCLASS METHODS" |
| 355 | The following example demonstrates how to override a superclass method and |
| 356 | then call the overridden method. The \fB\s-1SUPER\s0\fR pseudo-class allows the |
| 357 | programmer to call an overridden superclass method without actually knowing |
| 358 | where that method is defined. |
| 359 | .PP |
| 360 | .Vb 2 |
| 361 | \& package Buz; |
| 362 | \& sub goo { print "here's the goo\en" } |
| 363 | .Ve |
| 364 | .PP |
| 365 | .Vb 2 |
| 366 | \& package Bar; @ISA = qw( Buz ); |
| 367 | \& sub google { print "google here\en" } |
| 368 | .Ve |
| 369 | .PP |
| 370 | .Vb 2 |
| 371 | \& package Baz; |
| 372 | \& sub mumble { print "mumbling\en" } |
| 373 | .Ve |
| 374 | .PP |
| 375 | .Vb 2 |
| 376 | \& package Foo; |
| 377 | \& @ISA = qw( Bar Baz ); |
| 378 | .Ve |
| 379 | .PP |
| 380 | .Vb 17 |
| 381 | \& sub new { |
| 382 | \& my $type = shift; |
| 383 | \& bless [], $type; |
| 384 | \& } |
| 385 | \& sub grr { print "grumble\en" } |
| 386 | \& sub goo { |
| 387 | \& my $self = shift; |
| 388 | \& $self->SUPER::goo(); |
| 389 | \& } |
| 390 | \& sub mumble { |
| 391 | \& my $self = shift; |
| 392 | \& $self->SUPER::mumble(); |
| 393 | \& } |
| 394 | \& sub google { |
| 395 | \& my $self = shift; |
| 396 | \& $self->SUPER::google(); |
| 397 | \& } |
| 398 | .Ve |
| 399 | .PP |
| 400 | .Vb 1 |
| 401 | \& package main; |
| 402 | .Ve |
| 403 | .PP |
| 404 | .Vb 5 |
| 405 | \& $foo = Foo->new; |
| 406 | \& $foo->mumble; |
| 407 | \& $foo->grr; |
| 408 | \& $foo->goo; |
| 409 | \& $foo->google; |
| 410 | .Ve |
| 411 | .PP |
| 412 | Note that \f(CW\*(C`SUPER\*(C'\fR refers to the superclasses of the current package |
| 413 | (\f(CW\*(C`Foo\*(C'\fR), not to the superclasses of \f(CW$self\fR. |
| 414 | .SH "USING RELATIONSHIP WITH SDBM" |
| 415 | .IX Header "USING RELATIONSHIP WITH SDBM" |
| 416 | This example demonstrates an interface for the \s-1SDBM\s0 class. This creates a |
| 417 | \&\*(L"using\*(R" relationship between the \s-1SDBM\s0 class and the new class Mydbm. |
| 418 | .PP |
| 419 | .Vb 1 |
| 420 | \& package Mydbm; |
| 421 | .Ve |
| 422 | .PP |
| 423 | .Vb 3 |
| 424 | \& require SDBM_File; |
| 425 | \& require Tie::Hash; |
| 426 | \& @ISA = qw( Tie::Hash ); |
| 427 | .Ve |
| 428 | .PP |
| 429 | .Vb 19 |
| 430 | \& sub TIEHASH { |
| 431 | \& my $type = shift; |
| 432 | \& my $ref = SDBM_File->new(@_); |
| 433 | \& bless {'dbm' => $ref}, $type; |
| 434 | \& } |
| 435 | \& sub FETCH { |
| 436 | \& my $self = shift; |
| 437 | \& my $ref = $self->{'dbm'}; |
| 438 | \& $ref->FETCH(@_); |
| 439 | \& } |
| 440 | \& sub STORE { |
| 441 | \& my $self = shift; |
| 442 | \& if (defined $_[0]){ |
| 443 | \& my $ref = $self->{'dbm'}; |
| 444 | \& $ref->STORE(@_); |
| 445 | \& } else { |
| 446 | \& die "Cannot STORE an undefined key in Mydbm\en"; |
| 447 | \& } |
| 448 | \& } |
| 449 | .Ve |
| 450 | .PP |
| 451 | .Vb 2 |
| 452 | \& package main; |
| 453 | \& use Fcntl qw( O_RDWR O_CREAT ); |
| 454 | .Ve |
| 455 | .PP |
| 456 | .Vb 3 |
| 457 | \& tie %foo, "Mydbm", "Sdbm", O_RDWR|O_CREAT, 0640; |
| 458 | \& $foo{'bar'} = 123; |
| 459 | \& print "foo-bar = $foo{'bar'}\en"; |
| 460 | .Ve |
| 461 | .PP |
| 462 | .Vb 3 |
| 463 | \& tie %bar, "Mydbm", "Sdbm2", O_RDWR|O_CREAT, 0640; |
| 464 | \& $bar{'Cathy'} = 456; |
| 465 | \& print "bar-Cathy = $bar{'Cathy'}\en"; |
| 466 | .Ve |
| 467 | .SH "THINKING OF CODE REUSE" |
| 468 | .IX Header "THINKING OF CODE REUSE" |
| 469 | One strength of Object-Oriented languages is the ease with which old code |
| 470 | can use new code. The following examples will demonstrate first how one can |
| 471 | hinder code reuse and then how one can promote code reuse. |
| 472 | .PP |
| 473 | This first example illustrates a class which uses a fully-qualified method |
| 474 | call to access the \*(L"private\*(R" method \s-1\fIBAZ\s0()\fR. The second example will show |
| 475 | that it is impossible to override the \s-1\fIBAZ\s0()\fR method. |
| 476 | .PP |
| 477 | .Vb 1 |
| 478 | \& package FOO; |
| 479 | .Ve |
| 480 | .PP |
| 481 | .Vb 8 |
| 482 | \& sub new { |
| 483 | \& my $type = shift; |
| 484 | \& bless {}, $type; |
| 485 | \& } |
| 486 | \& sub bar { |
| 487 | \& my $self = shift; |
| 488 | \& $self->FOO::private::BAZ; |
| 489 | \& } |
| 490 | .Ve |
| 491 | .PP |
| 492 | .Vb 1 |
| 493 | \& package FOO::private; |
| 494 | .Ve |
| 495 | .PP |
| 496 | .Vb 3 |
| 497 | \& sub BAZ { |
| 498 | \& print "in BAZ\en"; |
| 499 | \& } |
| 500 | .Ve |
| 501 | .PP |
| 502 | .Vb 1 |
| 503 | \& package main; |
| 504 | .Ve |
| 505 | .PP |
| 506 | .Vb 2 |
| 507 | \& $a = FOO->new; |
| 508 | \& $a->bar; |
| 509 | .Ve |
| 510 | .PP |
| 511 | Now we try to override the \s-1\fIBAZ\s0()\fR method. We would like \fIFOO::bar()\fR to call |
| 512 | \&\s-1\fIGOOP::BAZ\s0()\fR, but this cannot happen because \fIFOO::bar()\fR explicitly calls |
| 513 | \&\fIFOO::private::BAZ()\fR. |
| 514 | .PP |
| 515 | .Vb 1 |
| 516 | \& package FOO; |
| 517 | .Ve |
| 518 | .PP |
| 519 | .Vb 8 |
| 520 | \& sub new { |
| 521 | \& my $type = shift; |
| 522 | \& bless {}, $type; |
| 523 | \& } |
| 524 | \& sub bar { |
| 525 | \& my $self = shift; |
| 526 | \& $self->FOO::private::BAZ; |
| 527 | \& } |
| 528 | .Ve |
| 529 | .PP |
| 530 | .Vb 1 |
| 531 | \& package FOO::private; |
| 532 | .Ve |
| 533 | .PP |
| 534 | .Vb 3 |
| 535 | \& sub BAZ { |
| 536 | \& print "in BAZ\en"; |
| 537 | \& } |
| 538 | .Ve |
| 539 | .PP |
| 540 | .Vb 6 |
| 541 | \& package GOOP; |
| 542 | \& @ISA = qw( FOO ); |
| 543 | \& sub new { |
| 544 | \& my $type = shift; |
| 545 | \& bless {}, $type; |
| 546 | \& } |
| 547 | .Ve |
| 548 | .PP |
| 549 | .Vb 3 |
| 550 | \& sub BAZ { |
| 551 | \& print "in GOOP::BAZ\en"; |
| 552 | \& } |
| 553 | .Ve |
| 554 | .PP |
| 555 | .Vb 1 |
| 556 | \& package main; |
| 557 | .Ve |
| 558 | .PP |
| 559 | .Vb 2 |
| 560 | \& $a = GOOP->new; |
| 561 | \& $a->bar; |
| 562 | .Ve |
| 563 | .PP |
| 564 | To create reusable code we must modify class \s-1FOO\s0, flattening class |
| 565 | FOO::private. The next example shows a reusable class \s-1FOO\s0 which allows the |
| 566 | method \s-1\fIGOOP::BAZ\s0()\fR to be used in place of \s-1\fIFOO::BAZ\s0()\fR. |
| 567 | .PP |
| 568 | .Vb 1 |
| 569 | \& package FOO; |
| 570 | .Ve |
| 571 | .PP |
| 572 | .Vb 8 |
| 573 | \& sub new { |
| 574 | \& my $type = shift; |
| 575 | \& bless {}, $type; |
| 576 | \& } |
| 577 | \& sub bar { |
| 578 | \& my $self = shift; |
| 579 | \& $self->BAZ; |
| 580 | \& } |
| 581 | .Ve |
| 582 | .PP |
| 583 | .Vb 3 |
| 584 | \& sub BAZ { |
| 585 | \& print "in BAZ\en"; |
| 586 | \& } |
| 587 | .Ve |
| 588 | .PP |
| 589 | .Vb 2 |
| 590 | \& package GOOP; |
| 591 | \& @ISA = qw( FOO ); |
| 592 | .Ve |
| 593 | .PP |
| 594 | .Vb 7 |
| 595 | \& sub new { |
| 596 | \& my $type = shift; |
| 597 | \& bless {}, $type; |
| 598 | \& } |
| 599 | \& sub BAZ { |
| 600 | \& print "in GOOP::BAZ\en"; |
| 601 | \& } |
| 602 | .Ve |
| 603 | .PP |
| 604 | .Vb 1 |
| 605 | \& package main; |
| 606 | .Ve |
| 607 | .PP |
| 608 | .Vb 2 |
| 609 | \& $a = GOOP->new; |
| 610 | \& $a->bar; |
| 611 | .Ve |
| 612 | .SH "CLASS CONTEXT AND THE OBJECT" |
| 613 | .IX Header "CLASS CONTEXT AND THE OBJECT" |
| 614 | Use the object to solve package and class context problems. Everything a |
| 615 | method needs should be available via the object or should be passed as a |
| 616 | parameter to the method. |
| 617 | .PP |
| 618 | A class will sometimes have static or global data to be used by the |
| 619 | methods. A subclass may want to override that data and replace it with new |
| 620 | data. When this happens the superclass may not know how to find the new |
| 621 | copy of the data. |
| 622 | .PP |
| 623 | This problem can be solved by using the object to define the context of the |
| 624 | method. Let the method look in the object for a reference to the data. The |
| 625 | alternative is to force the method to go hunting for the data (\*(L"Is it in my |
| 626 | class, or in a subclass? Which subclass?\*(R"), and this can be inconvenient |
| 627 | and will lead to hackery. It is better just to let the object tell the |
| 628 | method where that data is located. |
| 629 | .PP |
| 630 | .Vb 1 |
| 631 | \& package Bar; |
| 632 | .Ve |
| 633 | .PP |
| 634 | .Vb 1 |
| 635 | \& %fizzle = ( 'Password' => 'XYZZY' ); |
| 636 | .Ve |
| 637 | .PP |
| 638 | .Vb 6 |
| 639 | \& sub new { |
| 640 | \& my $type = shift; |
| 641 | \& my $self = {}; |
| 642 | \& $self->{'fizzle'} = \e%fizzle; |
| 643 | \& bless $self, $type; |
| 644 | \& } |
| 645 | .Ve |
| 646 | .PP |
| 647 | .Vb 2 |
| 648 | \& sub enter { |
| 649 | \& my $self = shift; |
| 650 | .Ve |
| 651 | .PP |
| 652 | .Vb 5 |
| 653 | \& # Don't try to guess if we should use %Bar::fizzle |
| 654 | \& # or %Foo::fizzle. The object already knows which |
| 655 | \& # we should use, so just ask it. |
| 656 | \& # |
| 657 | \& my $fizzle = $self->{'fizzle'}; |
| 658 | .Ve |
| 659 | .PP |
| 660 | .Vb 2 |
| 661 | \& print "The word is ", $fizzle->{'Password'}, "\en"; |
| 662 | \& } |
| 663 | .Ve |
| 664 | .PP |
| 665 | .Vb 2 |
| 666 | \& package Foo; |
| 667 | \& @ISA = qw( Bar ); |
| 668 | .Ve |
| 669 | .PP |
| 670 | .Vb 1 |
| 671 | \& %fizzle = ( 'Password' => 'Rumple' ); |
| 672 | .Ve |
| 673 | .PP |
| 674 | .Vb 6 |
| 675 | \& sub new { |
| 676 | \& my $type = shift; |
| 677 | \& my $self = Bar->new; |
| 678 | \& $self->{'fizzle'} = \e%fizzle; |
| 679 | \& bless $self, $type; |
| 680 | \& } |
| 681 | .Ve |
| 682 | .PP |
| 683 | .Vb 1 |
| 684 | \& package main; |
| 685 | .Ve |
| 686 | .PP |
| 687 | .Vb 4 |
| 688 | \& $a = Bar->new; |
| 689 | \& $b = Foo->new; |
| 690 | \& $a->enter; |
| 691 | \& $b->enter; |
| 692 | .Ve |
| 693 | .SH "INHERITING A CONSTRUCTOR" |
| 694 | .IX Header "INHERITING A CONSTRUCTOR" |
| 695 | An inheritable constructor should use the second form of \fIbless()\fR which allows |
| 696 | blessing directly into a specified class. Notice in this example that the |
| 697 | object will be a \s-1BAR\s0 not a \s-1FOO\s0, even though the constructor is in class \s-1FOO\s0. |
| 698 | .PP |
| 699 | .Vb 1 |
| 700 | \& package FOO; |
| 701 | .Ve |
| 702 | .PP |
| 703 | .Vb 5 |
| 704 | \& sub new { |
| 705 | \& my $type = shift; |
| 706 | \& my $self = {}; |
| 707 | \& bless $self, $type; |
| 708 | \& } |
| 709 | .Ve |
| 710 | .PP |
| 711 | .Vb 3 |
| 712 | \& sub baz { |
| 713 | \& print "in FOO::baz()\en"; |
| 714 | \& } |
| 715 | .Ve |
| 716 | .PP |
| 717 | .Vb 2 |
| 718 | \& package BAR; |
| 719 | \& @ISA = qw(FOO); |
| 720 | .Ve |
| 721 | .PP |
| 722 | .Vb 3 |
| 723 | \& sub baz { |
| 724 | \& print "in BAR::baz()\en"; |
| 725 | \& } |
| 726 | .Ve |
| 727 | .PP |
| 728 | .Vb 1 |
| 729 | \& package main; |
| 730 | .Ve |
| 731 | .PP |
| 732 | .Vb 2 |
| 733 | \& $a = BAR->new; |
| 734 | \& $a->baz; |
| 735 | .Ve |
| 736 | .SH "DELEGATION" |
| 737 | .IX Header "DELEGATION" |
| 738 | Some classes, such as SDBM_File, cannot be effectively subclassed because |
| 739 | they create foreign objects. Such a class can be extended with some sort of |
| 740 | aggregation technique such as the \*(L"using\*(R" relationship mentioned earlier or |
| 741 | by delegation. |
| 742 | .PP |
| 743 | The following example demonstrates delegation using an \s-1\fIAUTOLOAD\s0()\fR function to |
| 744 | perform message\-forwarding. This will allow the Mydbm object to behave |
| 745 | exactly like an SDBM_File object. The Mydbm class could now extend the |
| 746 | behavior by adding custom \s-1\fIFETCH\s0()\fR and \s-1\fISTORE\s0()\fR methods, if this is desired. |
| 747 | .PP |
| 748 | .Vb 1 |
| 749 | \& package Mydbm; |
| 750 | .Ve |
| 751 | .PP |
| 752 | .Vb 3 |
| 753 | \& require SDBM_File; |
| 754 | \& require Tie::Hash; |
| 755 | \& @ISA = qw(Tie::Hash); |
| 756 | .Ve |
| 757 | .PP |
| 758 | .Vb 5 |
| 759 | \& sub TIEHASH { |
| 760 | \& my $type = shift; |
| 761 | \& my $ref = SDBM_File->new(@_); |
| 762 | \& bless {'delegate' => $ref}; |
| 763 | \& } |
| 764 | .Ve |
| 765 | .PP |
| 766 | .Vb 2 |
| 767 | \& sub AUTOLOAD { |
| 768 | \& my $self = shift; |
| 769 | .Ve |
| 770 | .PP |
| 771 | .Vb 2 |
| 772 | \& # The Perl interpreter places the name of the |
| 773 | \& # message in a variable called $AUTOLOAD. |
| 774 | .Ve |
| 775 | .PP |
| 776 | .Vb 2 |
| 777 | \& # DESTROY messages should never be propagated. |
| 778 | \& return if $AUTOLOAD =~ /::DESTROY$/; |
| 779 | .Ve |
| 780 | .PP |
| 781 | .Vb 2 |
| 782 | \& # Remove the package name. |
| 783 | \& $AUTOLOAD =~ s/^Mydbm:://; |
| 784 | .Ve |
| 785 | .PP |
| 786 | .Vb 3 |
| 787 | \& # Pass the message to the delegate. |
| 788 | \& $self->{'delegate'}->$AUTOLOAD(@_); |
| 789 | \& } |
| 790 | .Ve |
| 791 | .PP |
| 792 | .Vb 2 |
| 793 | \& package main; |
| 794 | \& use Fcntl qw( O_RDWR O_CREAT ); |
| 795 | .Ve |
| 796 | .PP |
| 797 | .Vb 3 |
| 798 | \& tie %foo, "Mydbm", "adbm", O_RDWR|O_CREAT, 0640; |
| 799 | \& $foo{'bar'} = 123; |
| 800 | \& print "foo-bar = $foo{'bar'}\en"; |
| 801 | .Ve |
| 802 | .SH "SEE ALSO" |
| 803 | .IX Header "SEE ALSO" |
| 804 | perlboot, perltoot, perltooc. |