How does Perl polymorphism work?

I cannot understand how polymorphism works in Perl. I understand what polymorphism is, but I'm trying to understand how it works inside perl. Can someone point me to the documentation that explains this. All the search queries that I have done give me examples of what polymorphism is in perl, but not how perl makes it work.

+5
source share
5 answers

When a method is called on an object or class, Perl looks to see if the method can be provided directly by the class itself.

Because classes are just Perl packages , it's just a matter of finding the existence of a routine &Class::method.

, Perl @ISA (.. @Class::ISA), /, .

, , @ISA, .

, , Perl UNIVERSAL &UNIVERSAL::method.

, AUTOLOAD, .

.

+14

7 - Perl, , (2000) . .

, ++ Java # , "" Perl. , , Perl.

, , Perl , - , . : @ISA .

$o->bla. $o A, bla. B, C (@ISA = ('B', 'C')). B. . , . . , C , , , , UNIVERSAL bla.

+7

- , , * :

my $class = ref($_[0]);
my @isa = mro::get_linear_isa($class);
for my $pkg (@isa) {
   if (exists(&{$pkg.'::'.$method_name})) {
       return &{$pkg.'::'.$method_name};
   }
}

ref , . .

$ perl -MDevel::Peek -e'my $o = {}; Dump($o); bless($o, "SomeClass"); Dump($o);'
SV = IV(0x9e4ae0c) at 0x9e4ae10
  REFCNT = 1
  FLAGS = (PADMY,ROK)
  RV = 0x9e317d0
  SV = PVHV(0x9e36808) at 0x9e317d0
    REFCNT = 1
    FLAGS = (SHAREKEYS)
    ARRAY = 0x0
    KEYS = 0
    FILL = 0
    MAX = 7
    RITER = -1
    EITER = 0x0
SV = IV(0x9e4ae0c) at 0x9e4ae10
  REFCNT = 1
  FLAGS = (PADMY,ROK)
  RV = 0x9e317d0
  SV = PVHV(0x9e36808) at 0x9e317d0
    REFCNT = 1
    FLAGS = (OBJECT,SHAREKEYS)        <----
    STASH = 0x9e323d0   "SomeClass"   <----
    ARRAY = 0x0
    KEYS = 0
    FILL = 0
    MAX = 7
    RITER = -1
    EITER = 0x0

get_linear_isa @ISA $class @ISA , .

, Perl , .

* β€” , $method_name $class. , , , ISA, .

+4

- , . , Perl, " ".

, CGI, Apache2::Request Plack::Request param, HTTP-. , , param HTTP-, , .

, . Java Dog, Cat. , .

+1

, Perl. 12.5. Perl .

0

All Articles