From: Perl 5 Porters Date: Fri, 5 Jul 1996 02:55:24 +0000 (+0000) Subject: Add documentation for default UNIVERSAL methods X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=a2bdc9a53b6d7221bdf979c28003d54de36d16e5;p=p5sagit%2Fp5-mst-13.2.git Add documentation for default UNIVERSAL methods --- diff --git a/pod/perlobj.pod b/pod/perlobj.pod index 81c6c96..994edfe 100644 --- a/pod/perlobj.pod +++ b/pod/perlobj.pod @@ -137,7 +137,9 @@ that is called on behalf of the missing method. If neither a method nor an AUTOLOAD routine is found in @ISA, then one last try is made for the method (or an AUTOLOAD routine) in a class -called UNIVERSAL. If that doesn't work, Perl finally gives up and +called UNIVERSAL. (Several commonly used methods are automatically +supplied in the UNIVERSAL class; see L<"Default UNIVERSAL methods"> for +more details.) If that doesn't work, Perl finally gives up and complains. Perl classes only do method inheritance. Data inheritance is left @@ -267,7 +269,74 @@ with a simple scalar variable containing the method name: $method = $fast ? "findfirst" : "findbest"; $fred->$method(@args); -=head2 Destructors +=head2 Default UNIVERSAL methods + +The C package automatically contains the following methods that +are inherited by all other classes: + +=over 4 + +=item isa ( CLASS ) + +C returns I if its object is blessed into a sub-class of C + +C is also exportable and can be called as a sub with two arguments. This +allows the ability to check what a reference points to. Example + + use UNIVERSAL qw(isa); + + if(isa($ref, 'ARRAY')) { + ... + } + +=item can ( METHOD ) + +C checks to see if its object has a method called C, +if it does then a reference to the sub is returned, if it does not then +I is returned. + +=item require_version ( VERSION ) + +C will check that the current version of the package +is greater than C. This method is normally called as a static method. +This method is also called when the C form of C is used. + + use A 1.2 qw(some imported subs); + + A->require_version( 1.2 ); + +=item class () + +C returns the class name of its object. + +=item is_instance () + +C returns true if its object is an instance of some +class, false if its object is the class (package) itself. Example + + A->is_instance(); # False + + $var = 'A'; + $var->is_instance(); # False + + $ref = bless [], 'A'; + $ref->is_instance(); # True + +=item require_version ( [ VERSION ] ) + +C returns the VERSION number of the class (package). If +an argument is given then it will check that the current version is not +less that the given argument. + +=back + +B C directly uses Perl's internal code for method lookup, and +C uses a very similar method and cache-ing strategy. This may cause +strange effects if the Perl code dynamically changes @ISA in any package. + +You may add other methods to the UNIVERSAL class via Perl or XS code. + +=head2 Destructors When the last reference to an object goes away, the object is automatically destroyed. (This may even be after you exit, if you've