How can I override the function of a parent class with a child in Perl?

I would like to replace the parent function (Somefunc) in the child class, so when I call the main procedure, it does not work.

Is this possible in Perl?

the code:

package Test;

use strict;
use warnings;

sub Main()
{
    SomeFunc() or die "Somefunc returned 0";
}

sub SomeFunc()
{
    return 1;
}

package Test2;

use strict;
use warnings;

our @ISA = ("Test");

sub SomeFunc()
{
    return 0;
}

package main;

Test2->Main();
+5
source share
2 answers

When you call Test2->Main(), the package name is passed as the first parameter of the called function. You can use this parameter to access the correct function.

sub Main
{
    my ($class) = @_;
    $class->SomeFunc() or die "Somefunc returned 0";
}

$class "Test2", Test2->SomeFunc(). (.. bless Test::new, $self $class). Moose, - Perl.

+4

, , , , , ->. , , Test2->Main(), , OO-, .

package Test;

use strict;
use warnings;

sub Main
{
    my $class = shift;
    $class->SomeFunc() or die "Somefunc returned 0";
}

sub SomeFunc
{
    return 1;
}

package Test2;

our @ISA = ("Test");

sub SomeFunc
{
    return 0;
}

package main;

Test2->Main();

perlboot perltoot .

, parens , - , .

+3

All Articles