<p>How do I apply 'use base' in Perl to inherit subs from some base module?</p>

<p>I'm used to C++ inheritance mechanics, and all the sites I googled for this caused more confusion then help. I want to do something like the following:</p>

<pre><code>#! /usr/bin/perl
#The base class to inherit from
use strict;
use warnings;

package 'TestBase';

#-------------------------------
sub tbSub
{
    my ($self, $parm) = @_;
    print "\nTestBase: $parm\n";
}

1;
</code></pre>

<p>.</p>

<pre><code>#! /usr/bin/perl
#The descendent class
use strict;
use warnings;

use base qw(TestBase);
sub main;
sub mySub;

#-------------------------------
#Entry point...
main();

#---code------------------------
sub main
{
    mySub(1);
    tbSub(2);
    mySub(3);
}

#-------------------------------
sub mySub
{
    my $parm = shift;
    print "\nTester: $parm\n";
}
</code></pre>

<p>Perl complains/cannot find tbSub. </p>

Perl's inheritance inherits
methods
, not functions. That means you will have to call
```
```
main->tbSub(2);
```
```
However, what you really want is to inherit the method into a proper class:
```
```
package Derived;
use base "TestBase";

package main;
Derived->somemethod("foo");
```
```
Calling methods in the current package as functions won't pass in the $self or "this" object nor the class name magically. Internally,
```
```
Class->somemethod("foo")
```
```
essentially ends up being called as
```
```
Class::somemethod("Class", "foo")
```
```
internally. Of course, this assumes Class has a subroutine/method named "somemethod". If not, the superclasses of Class will be checked and if those don't have a method "somemethod" either, you'll get a fatal error. (Same logic applies for $obj->method("foo").)