Why does $ class-> SUPER :: new call the constructors of all parent classes when using multiple inheritance?

I am trying to use multiple inheritance in Perl, but I cannot figure out how to call multiple parent constructors from a child constructor.

A.pm:

package A;
use Carp qw (croak);
use strict;
use warnings;

sub new {
    my $class = shift;
    print "This is A new\n";
    my $self->{DEV_TYPE} = shift || "A";
    bless($self, $class);
    return $self;
}

sub a_func{
    print "This is A func\n";
}

1;

B.pm:

package B;
use Carp qw (croak);
use strict;
use warnings;

sub new {
    my $class = shift;
    print "This is B new\n";
    my $self->{DEV_TYPE} = shift || "B";
    bless($self, $class);
    return $self;
}

sub b_func{
    print "This is B func\n";
}

1;

C.pm:

package C;
use Carp qw (croak);
use strict;
use warnings;
eval "use A";
die $@ if $@;
eval "use B";
die $@ if $@;
our @ISA = ("A","B");

sub new {
    my $class = shift;
    my $self = $class->SUPER::new(@_);
    print "This is C new\n";
    $self->{DEV_TYPE} = shift || "C";
    bless($self, $class);
    return $self;
}

sub c_func{
    print "This is C func\n";
}

1;

B C::new, $class->SUPER::newdoes not call the constructor for B. If I call it explicitly with $class->B::new(@_);, I get an error

Unable to find object method "new" through package "B" under C.pm

What am I doing wrong?

+4
source share
1 answer

$class->SUPER::new A::new, A B @ISA. . perlobj:

, .

Perl - . , @ISA, , .. , @ISA .

, $class->SUPER::new . , , , .


B::new $class->B::new,

"" "B" C.pm

use B B . .


, parent pragma @ISA ,

use parent qw(Parent1 Parent2);

parent , use (, eval ing).

+7

Source: https://habr.com/ru/post/1672663/


All Articles