Below is a minimized example, where I have OA.pm (the parent object), OB.pm the child object, and the runner script:
Object OB:
package OB;
use OA;
require Exporter;
@ISA = (Exporter, OA);
@EXPORT = ();
sub new {
my $class = shift;
print $class->SUPER;
bless {}, $class;
};
1;
Object OA:
package OA;
require Exporter;
@ISA = (Exporter);
@EXPORT = ();
sub new {
bless {}, shift;
};
1;
And the runner:
#!/usr/bin/perl
use strict;
use warnings;
use OB;
print OB->new;
When I run it, I got:
Can't locate object method "SUPER" via package "OB" at OB.pm line 10.
1) What could be wrong?
2) And what would the SUPER points to if I have more than one parent?
3) Was use OA mandatory?
Unlike in Ruby, you must explicitly spell out the whole method you’re referring to.
That is assuming you wanted to call the method. If your intent is to figure out what parent class contains the new method, that’s complicated and I’m going to skip explaining how here.
If your class has more than one parent, multiple inheritance, Perl normally looks through the parents depth first for a matching method. This is often problematic, and you can switch to the C3 method resolution order (mro) with
use mro "c3"which avoids the diamond inheritance problem.More information on how SUPER and multiple inheritance.
Finally,
use OAorrequire OAwas mandatory in order to load the OA class. You can load and inherit from a class in one line usinguse parent "OA".