codygman
codygman

Reputation: 832

Do I need to use a class to use its methods in my subclass in Perl?

Alrighty, coding in Perl and just had a quick question. I have class created called SubtitleSite which is basically an abstraction, and a class called podnapisi that inherits SubtitleSite like this:

@ISA = qw(SubtitleSite);

My question is, do I have to use:

use SubtitleSite;

in order to have access to all the methods in SubtitleSite?

Upvotes: 4

Views: 143

Answers (5)

Ether
Ether

Reputation: 53966

If you are constructing an object in your child class, you can just call methods on yourself and they will be found through the magic of inheritance (see perldoc perlobj for more about SUPER):

sub foo
{
    my $this = shift;
    $this->method_on_parent;  # this works!
    $this->SUPER::foo;        # this works too
}

However if these classes are only library functions that don't use OO, you have to tell Perl explicitly where to find the function:

ParentClass::function;    # use the class name explicitly

Upvotes: 0

Geo
Geo

Reputation: 96817

In order to access the methods, you'd either have to inherit from it, or delegate to an object of it's type.

Upvotes: 0

Ronald Blaschke
Ronald Blaschke

Reputation: 4184

Yes, but most of the time you're better off not messing with @ISA directly. Just use parent qw(SubtitlesSite);, it will load SubtilteSite for you and add it to @ISA.

Upvotes: 11

Dana
Dana

Reputation: 2739

Yes.
Some more info can be found here:

Upvotes: 3

lexu
lexu

Reputation: 8849

YES.

Otherwise the symbols defined in SubtitleSite are undefined in podnapisi.

Upvotes: 2

Related Questions