| 1 | .\" Automatically generated by Pod::Man v1.34, Pod::Parser v1.13 |
| 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 "PERLOBJ 1" |
| 132 | .TH PERLOBJ 1 "2002-06-08" "perl v5.8.0" "Perl Programmers Reference Guide" |
| 133 | .SH "NAME" |
| 134 | perlobj \- Perl objects |
| 135 | .SH "DESCRIPTION" |
| 136 | .IX Header "DESCRIPTION" |
| 137 | First you need to understand what references are in Perl. |
| 138 | See perlref for that. Second, if you still find the following |
| 139 | reference work too complicated, a tutorial on object-oriented programming |
| 140 | in Perl can be found in perltoot and perltooc. |
| 141 | .PP |
| 142 | If you're still with us, then |
| 143 | here are three very simple definitions that you should find reassuring. |
| 144 | .IP "1." 4 |
| 145 | An object is simply a reference that happens to know which class it |
| 146 | belongs to. |
| 147 | .IP "2." 4 |
| 148 | A class is simply a package that happens to provide methods to deal |
| 149 | with object references. |
| 150 | .IP "3." 4 |
| 151 | A method is simply a subroutine that expects an object reference (or |
| 152 | a package name, for class methods) as the first argument. |
| 153 | .PP |
| 154 | We'll cover these points now in more depth. |
| 155 | .Sh "An Object is Simply a Reference" |
| 156 | .IX Subsection "An Object is Simply a Reference" |
| 157 | Unlike say \*(C+, Perl doesn't provide any special syntax for |
| 158 | constructors. A constructor is merely a subroutine that returns a |
| 159 | reference to something \*(L"blessed\*(R" into a class, generally the |
| 160 | class that the subroutine is defined in. Here is a typical |
| 161 | constructor: |
| 162 | .PP |
| 163 | .Vb 2 |
| 164 | \& package Critter; |
| 165 | \& sub new { bless {} } |
| 166 | .Ve |
| 167 | .PP |
| 168 | That word \f(CW\*(C`new\*(C'\fR isn't special. You could have written |
| 169 | a construct this way, too: |
| 170 | .PP |
| 171 | .Vb 2 |
| 172 | \& package Critter; |
| 173 | \& sub spawn { bless {} } |
| 174 | .Ve |
| 175 | .PP |
| 176 | This might even be preferable, because the \*(C+ programmers won't |
| 177 | be tricked into thinking that \f(CW\*(C`new\*(C'\fR works in Perl as it does in \*(C+. |
| 178 | It doesn't. We recommend that you name your constructors whatever |
| 179 | makes sense in the context of the problem you're solving. For example, |
| 180 | constructors in the Tk extension to Perl are named after the widgets |
| 181 | they create. |
| 182 | .PP |
| 183 | One thing that's different about Perl constructors compared with those in |
| 184 | \&\*(C+ is that in Perl, they have to allocate their own memory. (The other |
| 185 | things is that they don't automatically call overridden base-class |
| 186 | constructors.) The \f(CW\*(C`{}\*(C'\fR allocates an anonymous hash containing no |
| 187 | key/value pairs, and returns it The \fIbless()\fR takes that reference and |
| 188 | tells the object it references that it's now a Critter, and returns |
| 189 | the reference. This is for convenience, because the referenced object |
| 190 | itself knows that it has been blessed, and the reference to it could |
| 191 | have been returned directly, like this: |
| 192 | .PP |
| 193 | .Vb 5 |
| 194 | \& sub new { |
| 195 | \& my $self = {}; |
| 196 | \& bless $self; |
| 197 | \& return $self; |
| 198 | \& } |
| 199 | .Ve |
| 200 | .PP |
| 201 | You often see such a thing in more complicated constructors |
| 202 | that wish to call methods in the class as part of the construction: |
| 203 | .PP |
| 204 | .Vb 6 |
| 205 | \& sub new { |
| 206 | \& my $self = {}; |
| 207 | \& bless $self; |
| 208 | \& $self->initialize(); |
| 209 | \& return $self; |
| 210 | \& } |
| 211 | .Ve |
| 212 | .PP |
| 213 | If you care about inheritance (and you should; see |
| 214 | \&\*(L"Modules: Creation, Use, and Abuse\*(R" in perlmodlib), |
| 215 | then you want to use the two-arg form of bless |
| 216 | so that your constructors may be inherited: |
| 217 | .PP |
| 218 | .Vb 7 |
| 219 | \& sub new { |
| 220 | \& my $class = shift; |
| 221 | \& my $self = {}; |
| 222 | \& bless $self, $class; |
| 223 | \& $self->initialize(); |
| 224 | \& return $self; |
| 225 | \& } |
| 226 | .Ve |
| 227 | .PP |
| 228 | Or if you expect people to call not just \f(CW\*(C`CLASS\->new()\*(C'\fR but also |
| 229 | \&\f(CW\*(C`$obj\->new()\*(C'\fR, then use something like this. The \fIinitialize()\fR |
| 230 | method used will be of whatever \f(CW$class\fR we blessed the |
| 231 | object into: |
| 232 | .PP |
| 233 | .Vb 8 |
| 234 | \& sub new { |
| 235 | \& my $this = shift; |
| 236 | \& my $class = ref($this) || $this; |
| 237 | \& my $self = {}; |
| 238 | \& bless $self, $class; |
| 239 | \& $self->initialize(); |
| 240 | \& return $self; |
| 241 | \& } |
| 242 | .Ve |
| 243 | .PP |
| 244 | Within the class package, the methods will typically deal with the |
| 245 | reference as an ordinary reference. Outside the class package, |
| 246 | the reference is generally treated as an opaque value that may |
| 247 | be accessed only through the class's methods. |
| 248 | .PP |
| 249 | Although a constructor can in theory re-bless a referenced object |
| 250 | currently belonging to another class, this is almost certainly going |
| 251 | to get you into trouble. The new class is responsible for all |
| 252 | cleanup later. The previous blessing is forgotten, as an object |
| 253 | may belong to only one class at a time. (Although of course it's |
| 254 | free to inherit methods from many classes.) If you find yourself |
| 255 | having to do this, the parent class is probably misbehaving, though. |
| 256 | .PP |
| 257 | A clarification: Perl objects are blessed. References are not. Objects |
| 258 | know which package they belong to. References do not. The \fIbless()\fR |
| 259 | function uses the reference to find the object. Consider |
| 260 | the following example: |
| 261 | .PP |
| 262 | .Vb 4 |
| 263 | \& $a = {}; |
| 264 | \& $b = $a; |
| 265 | \& bless $a, BLAH; |
| 266 | \& print "\e$b is a ", ref($b), "\en"; |
| 267 | .Ve |
| 268 | .PP |
| 269 | This reports \f(CW$b\fR as being a \s-1BLAH\s0, so obviously \fIbless()\fR |
| 270 | operated on the object and not on the reference. |
| 271 | .Sh "A Class is Simply a Package" |
| 272 | .IX Subsection "A Class is Simply a Package" |
| 273 | Unlike say \*(C+, Perl doesn't provide any special syntax for class |
| 274 | definitions. You use a package as a class by putting method |
| 275 | definitions into the class. |
| 276 | .PP |
| 277 | There is a special array within each package called \f(CW@ISA\fR, which says |
| 278 | where else to look for a method if you can't find it in the current |
| 279 | package. This is how Perl implements inheritance. Each element of the |
| 280 | \&\f(CW@ISA\fR array is just the name of another package that happens to be a |
| 281 | class package. The classes are searched (depth first) for missing |
| 282 | methods in the order that they occur in \f(CW@ISA\fR. The classes accessible |
| 283 | through \f(CW@ISA\fR are known as base classes of the current class. |
| 284 | .PP |
| 285 | All classes implicitly inherit from class \f(CW\*(C`UNIVERSAL\*(C'\fR as their |
| 286 | last base class. Several commonly used methods are automatically |
| 287 | supplied in the \s-1UNIVERSAL\s0 class; see \*(L"Default \s-1UNIVERSAL\s0 methods\*(R" for |
| 288 | more details. |
| 289 | .PP |
| 290 | If a missing method is found in a base class, it is cached |
| 291 | in the current class for efficiency. Changing \f(CW@ISA\fR or defining new |
| 292 | subroutines invalidates the cache and causes Perl to do the lookup again. |
| 293 | .PP |
| 294 | If neither the current class, its named base classes, nor the \s-1UNIVERSAL\s0 |
| 295 | class contains the requested method, these three places are searched |
| 296 | all over again, this time looking for a method named \s-1\fIAUTOLOAD\s0()\fR. If an |
| 297 | \&\s-1AUTOLOAD\s0 is found, this method is called on behalf of the missing method, |
| 298 | setting the package global \f(CW$AUTOLOAD\fR to be the fully qualified name of |
| 299 | the method that was intended to be called. |
| 300 | .PP |
| 301 | If none of that works, Perl finally gives up and complains. |
| 302 | .PP |
| 303 | If you want to stop the \s-1AUTOLOAD\s0 inheritance say simply |
| 304 | .PP |
| 305 | .Vb 1 |
| 306 | \& sub AUTOLOAD; |
| 307 | .Ve |
| 308 | .PP |
| 309 | and the call will die using the name of the sub being called. |
| 310 | .PP |
| 311 | Perl classes do method inheritance only. Data inheritance is left up |
| 312 | to the class itself. By and large, this is not a problem in Perl, |
| 313 | because most classes model the attributes of their object using an |
| 314 | anonymous hash, which serves as its own little namespace to be carved up |
| 315 | by the various classes that might want to do something with the object. |
| 316 | The only problem with this is that you can't sure that you aren't using |
| 317 | a piece of the hash that isn't already used. A reasonable workaround |
| 318 | is to prepend your fieldname in the hash with the package name. |
| 319 | .PP |
| 320 | .Vb 4 |
| 321 | \& sub bump { |
| 322 | \& my $self = shift; |
| 323 | \& $self->{ __PACKAGE__ . ".count"}++; |
| 324 | \& } |
| 325 | .Ve |
| 326 | .Sh "A Method is Simply a Subroutine" |
| 327 | .IX Subsection "A Method is Simply a Subroutine" |
| 328 | Unlike say \*(C+, Perl doesn't provide any special syntax for method |
| 329 | definition. (It does provide a little syntax for method invocation |
| 330 | though. More on that later.) A method expects its first argument |
| 331 | to be the object (reference) or package (string) it is being invoked |
| 332 | on. There are two ways of calling methods, which we'll call class |
| 333 | methods and instance methods. |
| 334 | .PP |
| 335 | A class method expects a class name as the first argument. It |
| 336 | provides functionality for the class as a whole, not for any |
| 337 | individual object belonging to the class. Constructors are often |
| 338 | class methods, but see perltoot and perltooc for alternatives. |
| 339 | Many class methods simply ignore their first argument, because they |
| 340 | already know what package they're in and don't care what package |
| 341 | they were invoked via. (These aren't necessarily the same, because |
| 342 | class methods follow the inheritance tree just like ordinary instance |
| 343 | methods.) Another typical use for class methods is to look up an |
| 344 | object by name: |
| 345 | .PP |
| 346 | .Vb 4 |
| 347 | \& sub find { |
| 348 | \& my ($class, $name) = @_; |
| 349 | \& $objtable{$name}; |
| 350 | \& } |
| 351 | .Ve |
| 352 | .PP |
| 353 | An instance method expects an object reference as its first argument. |
| 354 | Typically it shifts the first argument into a \*(L"self\*(R" or \*(L"this\*(R" variable, |
| 355 | and then uses that as an ordinary reference. |
| 356 | .PP |
| 357 | .Vb 7 |
| 358 | \& sub display { |
| 359 | \& my $self = shift; |
| 360 | \& my @keys = @_ ? @_ : sort keys %$self; |
| 361 | \& foreach $key (@keys) { |
| 362 | \& print "\et$key => $self->{$key}\en"; |
| 363 | \& } |
| 364 | \& } |
| 365 | .Ve |
| 366 | .Sh "Method Invocation" |
| 367 | .IX Subsection "Method Invocation" |
| 368 | For various historical and other reasons, Perl offers two equivalent |
| 369 | ways to write a method call. The simpler and more common way is to use |
| 370 | the arrow notation: |
| 371 | .PP |
| 372 | .Vb 2 |
| 373 | \& my $fred = Critter->find("Fred"); |
| 374 | \& $fred->display("Height", "Weight"); |
| 375 | .Ve |
| 376 | .PP |
| 377 | You should already be familiar with the use of the \f(CW\*(C`\->\*(C'\fR operator with |
| 378 | references. In fact, since \f(CW$fred\fR above is a reference to an object, |
| 379 | you could think of the method call as just another form of |
| 380 | dereferencing. |
| 381 | .PP |
| 382 | Whatever is on the left side of the arrow, whether a reference or a |
| 383 | class name, is passed to the method subroutine as its first argument. |
| 384 | So the above code is mostly equivalent to: |
| 385 | .PP |
| 386 | .Vb 2 |
| 387 | \& my $fred = Critter::find("Critter", "Fred"); |
| 388 | \& Critter::display($fred, "Height", "Weight"); |
| 389 | .Ve |
| 390 | .PP |
| 391 | How does Perl know which package the subroutine is in? By looking at |
| 392 | the left side of the arrow, which must be either a package name or a |
| 393 | reference to an object, i.e. something that has been blessed to a |
| 394 | package. Either way, that's the package where Perl starts looking. If |
| 395 | that package has no subroutine with that name, Perl starts looking for |
| 396 | it in any base classes of that package, and so on. |
| 397 | .PP |
| 398 | If you need to, you \fIcan\fR force Perl to start looking in some other package: |
| 399 | .PP |
| 400 | .Vb 2 |
| 401 | \& my $barney = MyCritter->Critter::find("Barney"); |
| 402 | \& $barney->Critter::display("Height", "Weight"); |
| 403 | .Ve |
| 404 | .PP |
| 405 | Here \f(CW\*(C`MyCritter\*(C'\fR is presumably a subclass of \f(CW\*(C`Critter\*(C'\fR that defines |
| 406 | its own versions of \fIfind()\fR and \fIdisplay()\fR. We haven't specified what |
| 407 | those methods do, but that doesn't matter above since we've forced Perl |
| 408 | to start looking for the subroutines in \f(CW\*(C`Critter\*(C'\fR. |
| 409 | .PP |
| 410 | As a special case of the above, you may use the \f(CW\*(C`SUPER\*(C'\fR pseudo-class to |
| 411 | tell Perl to start looking for the method in the packages named in the |
| 412 | current class's \f(CW@ISA\fR list. |
| 413 | .PP |
| 414 | .Vb 2 |
| 415 | \& package MyCritter; |
| 416 | \& use base 'Critter'; # sets @MyCritter::ISA = ('Critter'); |
| 417 | .Ve |
| 418 | .PP |
| 419 | .Vb 4 |
| 420 | \& sub display { |
| 421 | \& my ($self, @args) = @_; |
| 422 | \& $self->SUPER::display("Name", @args); |
| 423 | \& } |
| 424 | .Ve |
| 425 | .PP |
| 426 | Instead of a class name or an object reference, you can also use any |
| 427 | expression that returns either of those on the left side of the arrow. |
| 428 | So the following statement is valid: |
| 429 | .PP |
| 430 | .Vb 1 |
| 431 | \& Critter->find("Fred")->display("Height", "Weight"); |
| 432 | .Ve |
| 433 | .PP |
| 434 | and so is the following: |
| 435 | .PP |
| 436 | .Vb 1 |
| 437 | \& my $fred = (reverse "rettirC")->find(reverse "derF"); |
| 438 | .Ve |
| 439 | .Sh "Indirect Object Syntax" |
| 440 | .IX Subsection "Indirect Object Syntax" |
| 441 | The other way to invoke a method is by using the so-called \*(L"indirect |
| 442 | object\*(R" notation. This syntax was available in Perl 4 long before |
| 443 | objects were introduced, and is still used with filehandles like this: |
| 444 | .PP |
| 445 | .Vb 1 |
| 446 | \& print STDERR "help!!!\en"; |
| 447 | .Ve |
| 448 | .PP |
| 449 | The same syntax can be used to call either object or class methods. |
| 450 | .PP |
| 451 | .Vb 2 |
| 452 | \& my $fred = find Critter "Fred"; |
| 453 | \& display $fred "Height", "Weight"; |
| 454 | .Ve |
| 455 | .PP |
| 456 | Notice that there is no comma between the object or class name and the |
| 457 | parameters. This is how Perl can tell you want an indirect method call |
| 458 | instead of an ordinary subroutine call. |
| 459 | .PP |
| 460 | But what if there are no arguments? In that case, Perl must guess what |
| 461 | you want. Even worse, it must make that guess \fIat compile time\fR. |
| 462 | Usually Perl gets it right, but when it doesn't you get a function |
| 463 | call compiled as a method, or vice versa. This can introduce subtle bugs |
| 464 | that are hard to detect. |
| 465 | .PP |
| 466 | For example, a call to a method \f(CW\*(C`new\*(C'\fR in indirect notation \*(-- as \*(C+ |
| 467 | programmers are wont to make \*(-- can be miscompiled into a subroutine |
| 468 | call if there's already a \f(CW\*(C`new\*(C'\fR function in scope. You'd end up |
| 469 | calling the current package's \f(CW\*(C`new\*(C'\fR as a subroutine, rather than the |
| 470 | desired class's method. The compiler tries to cheat by remembering |
| 471 | bareword \f(CW\*(C`require\*(C'\fRs, but the grief when it messes up just isn't worth the |
| 472 | years of debugging it will take you to track down such subtle bugs. |
| 473 | .PP |
| 474 | There is another problem with this syntax: the indirect object is |
| 475 | limited to a name, a scalar variable, or a block, because it would have |
| 476 | to do too much lookahead otherwise, just like any other postfix |
| 477 | dereference in the language. (These are the same quirky rules as are |
| 478 | used for the filehandle slot in functions like \f(CW\*(C`print\*(C'\fR and \f(CW\*(C`printf\*(C'\fR.) |
| 479 | This can lead to horribly confusing precedence problems, as in these |
| 480 | next two lines: |
| 481 | .PP |
| 482 | .Vb 2 |
| 483 | \& move $obj->{FIELD}; # probably wrong! |
| 484 | \& move $ary[$i]; # probably wrong! |
| 485 | .Ve |
| 486 | .PP |
| 487 | Those actually parse as the very surprising: |
| 488 | .PP |
| 489 | .Vb 2 |
| 490 | \& $obj->move->{FIELD}; # Well, lookee here |
| 491 | \& $ary->move([$i]); # Didn't expect this one, eh? |
| 492 | .Ve |
| 493 | .PP |
| 494 | Rather than what you might have expected: |
| 495 | .PP |
| 496 | .Vb 2 |
| 497 | \& $obj->{FIELD}->move(); # You should be so lucky. |
| 498 | \& $ary[$i]->move; # Yeah, sure. |
| 499 | .Ve |
| 500 | .PP |
| 501 | To get the correct behavior with indirect object syntax, you would have |
| 502 | to use a block around the indirect object: |
| 503 | .PP |
| 504 | .Vb 2 |
| 505 | \& move {$obj->{FIELD}}; |
| 506 | \& move {$ary[$i]}; |
| 507 | .Ve |
| 508 | .PP |
| 509 | Even then, you still have the same potential problem if there happens to |
| 510 | be a function named \f(CW\*(C`move\*(C'\fR in the current package. \fBThe \f(CB\*(C`\->\*(C'\fB |
| 511 | notation suffers from neither of these disturbing ambiguities, so we |
| 512 | recommend you use it exclusively.\fR However, you may still end up having |
| 513 | to read code using the indirect object notation, so it's important to be |
| 514 | familiar with it. |
| 515 | .Sh "Default \s-1UNIVERSAL\s0 methods" |
| 516 | .IX Subsection "Default UNIVERSAL methods" |
| 517 | The \f(CW\*(C`UNIVERSAL\*(C'\fR package automatically contains the following methods that |
| 518 | are inherited by all other classes: |
| 519 | .IP "isa(\s-1CLASS\s0)" 4 |
| 520 | .IX Item "isa(CLASS)" |
| 521 | \&\f(CW\*(C`isa\*(C'\fR returns \fItrue\fR if its object is blessed into a subclass of \f(CW\*(C`CLASS\*(C'\fR |
| 522 | .Sp |
| 523 | You can also call \f(CW\*(C`UNIVERSAL::isa\*(C'\fR as a subroutine with two arguments. |
| 524 | The first does not need to be an object or even a reference. This |
| 525 | allows you to check what a reference points to, or whether |
| 526 | something is a reference of a given type. Example |
| 527 | .Sp |
| 528 | .Vb 3 |
| 529 | \& if(UNIVERSAL::isa($ref, 'ARRAY')) { |
| 530 | \& #... |
| 531 | \& } |
| 532 | .Ve |
| 533 | .Sp |
| 534 | To determine if a reference is a blessed object, you can write |
| 535 | .Sp |
| 536 | .Vb 1 |
| 537 | \& print "It's an object\en" if UNIVERSAL::isa($val, 'UNIVERSAL'); |
| 538 | .Ve |
| 539 | .IP "can(\s-1METHOD\s0)" 4 |
| 540 | .IX Item "can(METHOD)" |
| 541 | \&\f(CW\*(C`can\*(C'\fR checks to see if its object has a method called \f(CW\*(C`METHOD\*(C'\fR, |
| 542 | if it does then a reference to the sub is returned, if it does not then |
| 543 | \&\fIundef\fR is returned. |
| 544 | .Sp |
| 545 | \&\f(CW\*(C`UNIVERSAL::can\*(C'\fR can also be called as a subroutine with two arguments. |
| 546 | It'll always return \fIundef\fR if its first argument isn't an object or a |
| 547 | class name. So here's another way to check if a reference is a |
| 548 | blessed object |
| 549 | .Sp |
| 550 | .Vb 1 |
| 551 | \& print "It's still an object\en" if UNIVERSAL::can($val, 'can'); |
| 552 | .Ve |
| 553 | .Sp |
| 554 | You can also use the \f(CW\*(C`blessed\*(C'\fR function of Scalar::Util: |
| 555 | .Sp |
| 556 | .Vb 1 |
| 557 | \& use Scalar::Util 'blessed'; |
| 558 | .Ve |
| 559 | .Sp |
| 560 | .Vb 1 |
| 561 | \& my $blessing = blessed $suspected_object; |
| 562 | .Ve |
| 563 | .Sp |
| 564 | \&\f(CW\*(C`blessed\*(C'\fR returns the name of the package the argument has been |
| 565 | blessed into, or \f(CW\*(C`undef\*(C'\fR. |
| 566 | .IP "\s-1VERSION\s0( [\s-1NEED\s0] )" 4 |
| 567 | .IX Item "VERSION( [NEED] )" |
| 568 | \&\f(CW\*(C`VERSION\*(C'\fR returns the version number of the class (package). If the |
| 569 | \&\s-1NEED\s0 argument is given then it will check that the current version (as |
| 570 | defined by the \f(CW$VERSION\fR variable in the given package) not less than |
| 571 | \&\s-1NEED\s0; it will die if this is not the case. This method is normally |
| 572 | called as a class method. This method is called automatically by the |
| 573 | \&\f(CW\*(C`VERSION\*(C'\fR form of \f(CW\*(C`use\*(C'\fR. |
| 574 | .Sp |
| 575 | .Vb 3 |
| 576 | \& use A 1.2 qw(some imported subs); |
| 577 | \& # implies: |
| 578 | \& A->VERSION(1.2); |
| 579 | .Ve |
| 580 | .PP |
| 581 | \&\fB\s-1NOTE:\s0\fR \f(CW\*(C`can\*(C'\fR directly uses Perl's internal code for method lookup, and |
| 582 | \&\f(CW\*(C`isa\*(C'\fR uses a very similar method and cache-ing strategy. This may cause |
| 583 | strange effects if the Perl code dynamically changes \f(CW@ISA\fR in any package. |
| 584 | .PP |
| 585 | You may add other methods to the \s-1UNIVERSAL\s0 class via Perl or \s-1XS\s0 code. |
| 586 | You do not need to \f(CW\*(C`use UNIVERSAL\*(C'\fR to make these methods |
| 587 | available to your program (and you should not do so). |
| 588 | .Sh "Destructors" |
| 589 | .IX Subsection "Destructors" |
| 590 | When the last reference to an object goes away, the object is |
| 591 | automatically destroyed. (This may even be after you exit, if you've |
| 592 | stored references in global variables.) If you want to capture control |
| 593 | just before the object is freed, you may define a \s-1DESTROY\s0 method in |
| 594 | your class. It will automatically be called at the appropriate moment, |
| 595 | and you can do any extra cleanup you need to do. Perl passes a reference |
| 596 | to the object under destruction as the first (and only) argument. Beware |
| 597 | that the reference is a read-only value, and cannot be modified by |
| 598 | manipulating \f(CW$_[0]\fR within the destructor. The object itself (i.e. |
| 599 | the thingy the reference points to, namely \f(CW\*(C`${$_[0]}\*(C'\fR, \f(CW\*(C`@{$_[0]}\*(C'\fR, |
| 600 | \&\f(CW\*(C`%{$_[0]}\*(C'\fR etc.) is not similarly constrained. |
| 601 | .PP |
| 602 | If you arrange to re-bless the reference before the destructor returns, |
| 603 | perl will again call the \s-1DESTROY\s0 method for the re-blessed object after |
| 604 | the current one returns. This can be used for clean delegation of |
| 605 | object destruction, or for ensuring that destructors in the base classes |
| 606 | of your choosing get called. Explicitly calling \s-1DESTROY\s0 is also possible, |
| 607 | but is usually never needed. |
| 608 | .PP |
| 609 | Do not confuse the previous discussion with how objects \fI\s-1CONTAINED\s0\fR in the current |
| 610 | one are destroyed. Such objects will be freed and destroyed automatically |
| 611 | when the current object is freed, provided no other references to them exist |
| 612 | elsewhere. |
| 613 | .Sh "Summary" |
| 614 | .IX Subsection "Summary" |
| 615 | That's about all there is to it. Now you need just to go off and buy a |
| 616 | book about object-oriented design methodology, and bang your forehead |
| 617 | with it for the next six months or so. |
| 618 | .Sh "Two-Phased Garbage Collection" |
| 619 | .IX Subsection "Two-Phased Garbage Collection" |
| 620 | For most purposes, Perl uses a fast and simple, reference-based |
| 621 | garbage collection system. That means there's an extra |
| 622 | dereference going on at some level, so if you haven't built |
| 623 | your Perl executable using your C compiler's \f(CW\*(C`\-O\*(C'\fR flag, performance |
| 624 | will suffer. If you \fIhave\fR built Perl with \f(CW\*(C`cc \-O\*(C'\fR, then this |
| 625 | probably won't matter. |
| 626 | .PP |
| 627 | A more serious concern is that unreachable memory with a non-zero |
| 628 | reference count will not normally get freed. Therefore, this is a bad |
| 629 | idea: |
| 630 | .PP |
| 631 | .Vb 4 |
| 632 | \& { |
| 633 | \& my $a; |
| 634 | \& $a = \e$a; |
| 635 | \& } |
| 636 | .Ve |
| 637 | .PP |
| 638 | Even thought \f(CW$a\fR \fIshould\fR go away, it can't. When building recursive data |
| 639 | structures, you'll have to break the self-reference yourself explicitly |
| 640 | if you don't care to leak. For example, here's a self-referential |
| 641 | node such as one might use in a sophisticated tree structure: |
| 642 | .PP |
| 643 | .Vb 8 |
| 644 | \& sub new_node { |
| 645 | \& my $self = shift; |
| 646 | \& my $class = ref($self) || $self; |
| 647 | \& my $node = {}; |
| 648 | \& $node->{LEFT} = $node->{RIGHT} = $node; |
| 649 | \& $node->{DATA} = [ @_ ]; |
| 650 | \& return bless $node => $class; |
| 651 | \& } |
| 652 | .Ve |
| 653 | .PP |
| 654 | If you create nodes like that, they (currently) won't go away unless you |
| 655 | break their self reference yourself. (In other words, this is not to be |
| 656 | construed as a feature, and you shouldn't depend on it.) |
| 657 | .PP |
| 658 | Almost. |
| 659 | .PP |
| 660 | When an interpreter thread finally shuts down (usually when your program |
| 661 | exits), then a rather costly but complete mark-and-sweep style of garbage |
| 662 | collection is performed, and everything allocated by that thread gets |
| 663 | destroyed. This is essential to support Perl as an embedded or a |
| 664 | multithreadable language. For example, this program demonstrates Perl's |
| 665 | two-phased garbage collection: |
| 666 | .PP |
| 667 | .Vb 2 |
| 668 | \& #!/usr/bin/perl |
| 669 | \& package Subtle; |
| 670 | .Ve |
| 671 | .PP |
| 672 | .Vb 6 |
| 673 | \& sub new { |
| 674 | \& my $test; |
| 675 | \& $test = \e$test; |
| 676 | \& warn "CREATING " . \e$test; |
| 677 | \& return bless \e$test; |
| 678 | \& } |
| 679 | .Ve |
| 680 | .PP |
| 681 | .Vb 4 |
| 682 | \& sub DESTROY { |
| 683 | \& my $self = shift; |
| 684 | \& warn "DESTROYING $self"; |
| 685 | \& } |
| 686 | .Ve |
| 687 | .PP |
| 688 | .Vb 1 |
| 689 | \& package main; |
| 690 | .Ve |
| 691 | .PP |
| 692 | .Vb 7 |
| 693 | \& warn "starting program"; |
| 694 | \& { |
| 695 | \& my $a = Subtle->new; |
| 696 | \& my $b = Subtle->new; |
| 697 | \& $$a = 0; # break selfref |
| 698 | \& warn "leaving block"; |
| 699 | \& } |
| 700 | .Ve |
| 701 | .PP |
| 702 | .Vb 3 |
| 703 | \& warn "just exited block"; |
| 704 | \& warn "time to die..."; |
| 705 | \& exit; |
| 706 | .Ve |
| 707 | .PP |
| 708 | When run as \fI/tmp/test\fR, the following output is produced: |
| 709 | .PP |
| 710 | .Vb 8 |
| 711 | \& starting program at /tmp/test line 18. |
| 712 | \& CREATING SCALAR(0x8e5b8) at /tmp/test line 7. |
| 713 | \& CREATING SCALAR(0x8e57c) at /tmp/test line 7. |
| 714 | \& leaving block at /tmp/test line 23. |
| 715 | \& DESTROYING Subtle=SCALAR(0x8e5b8) at /tmp/test line 13. |
| 716 | \& just exited block at /tmp/test line 26. |
| 717 | \& time to die... at /tmp/test line 27. |
| 718 | \& DESTROYING Subtle=SCALAR(0x8e57c) during global destruction. |
| 719 | .Ve |
| 720 | .PP |
| 721 | Notice that \*(L"global destruction\*(R" bit there? That's the thread |
| 722 | garbage collector reaching the unreachable. |
| 723 | .PP |
| 724 | Objects are always destructed, even when regular refs aren't. Objects |
| 725 | are destructed in a separate pass before ordinary refs just to |
| 726 | prevent object destructors from using refs that have been themselves |
| 727 | destructed. Plain refs are only garbage-collected if the destruct level |
| 728 | is greater than 0. You can test the higher levels of global destruction |
| 729 | by setting the \s-1PERL_DESTRUCT_LEVEL\s0 environment variable, presuming |
| 730 | \&\f(CW\*(C`\-DDEBUGGING\*(C'\fR was enabled during perl build time. |
| 731 | See \*(L"\s-1PERL_DESTRUCT_LEVEL\s0\*(R" in perlhack for more information. |
| 732 | .PP |
| 733 | A more complete garbage collection strategy will be implemented |
| 734 | at a future date. |
| 735 | .PP |
| 736 | In the meantime, the best solution is to create a non-recursive container |
| 737 | class that holds a pointer to the self-referential data structure. |
| 738 | Define a \s-1DESTROY\s0 method for the containing object's class that manually |
| 739 | breaks the circularities in the self-referential structure. |
| 740 | .SH "SEE ALSO" |
| 741 | .IX Header "SEE ALSO" |
| 742 | A kinder, gentler tutorial on object-oriented programming in Perl can |
| 743 | be found in perltoot, perlboot and perltooc. You should |
| 744 | also check out perlbot for other object tricks, traps, and tips, as |
| 745 | well as perlmodlib for some style guides on constructing both |
| 746 | modules and classes. |