I would like to replace parent function (Somefunc) in child class, so when I call Main procedure it should fail.
Is it possible in Perl?
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();
When you call
Test2->Main(), the package name is passed as the first parameter to the called function. You can use the parameter to address the right function.In this example,
$classwill be"Test2", so you will callTest2->SomeFunc(). Even better solution would be to use instances (i.e.,blessthe object inTest::new, use$selfinstead of$class). And even better would be to useMoose, which solves a lot of problems with object-oriented programming in Perl.