joslinm
joslinm

Reputation: 8105

How do I inherit functions into an object and allow them to be used from given object?

I'm having trouble putting this question to better words, but how might I accomplish something like this:

FileSpecClone.pm
package FileSpecClone;
use File::Spec::Unix;

sub new() {
bless {};
}

CloneScript.pl
use FileSpecClone;
$obj = FileSpecClone->new();
# A FileSpec::Unix subroutine
$obj->catpath('a','b','c');

Upvotes: 1

Views: 81

Answers (1)

mob
mob

Reputation: 118595

You must specify that FileSpecClone should inherit the methods of File::Spec::Unix by setting the package @ISA variable.

package FileSpecClone;
use File::Spec::Unix;
our @ISA = qw(File::Spec::Unix);
...

This is documented in perlobj.

If you have the parent module (a core module since v5.10), that will handle the details of setting @ISA at compile time. (HT: daxim)

use parent 'File::Spec::Unix';

Upvotes: 3

Related Questions