-1

Question regarding inheritance in Perl using @ISA: Input - 3 files: one is a main script, two containing parent & child packages, correspondingly:

main:

#!/usr/bin/perl

use child qw(parent_or_child_function srictly_parent_function);

parent_or_child_function();
srictly_parent_function();

parent.pm:

package parent;

sub srictly_parent_function
{
    print "this is strictly parent function\n";
}

sub parent_or_child_function
{
    print "this is parent function which can be inherited\n";
}

1;

child.pm:

package child;

our @ISA = 'parent';

use Exporter qw(import);
@EXPORT_OK = qw(parent_or_child_function srictly_parent_function);

sub parent_or_child_function
{
    print "this is child function that replaced parent's\n";
}

1;

Output is:

 $main
this is child function that replaced parent's
Undefined subroutine &child::srictly_parent_function called at main line 6.

What am I doing wrong? I understand that child package doesn't have strictly_parent_function , but shouldn't child's @ISA package be searched for it?

1
  • 1
    Re "What am I doing wrong?", 1) Inheritance is a feature of objects, yet you have no objects. 2) You export &child::srictly_parent_function, yet you never define it.
    – ikegami
    Aug 13, 2014 at 19:23

1 Answer 1

1

Firstly, make parent actually an object.

package parent;
use strict;
use warnings;

# Constructor
sub new {
    my ($proto) = @_;
    my $class = ref($proto) || $proto;
    my $self = {};

    # Bless is what casts $self (instance of this class) as an object
    return bless($self, $class);
}

sub srictly_parent_function {
    my ($self) = @_;
    print "this is strictly parent function\n";
}

sub parent_or_child_function {
    my ($self) = @_;
    print "this is parent function which can be inherited\n";
}

1;

Then with parent as an object, child can inherit

package child;

use strict;
use warnings;

# I prefer use base, as it's safer than pushing classes into @ISA
# See http://docstore.mik.ua/orelly/perl2/prog/ch31_03.htm)
use base qw(parent);

sub parent_or_child_function {
    my ($self) = @_;
    print "this is child function that replaced parent's\n";
}

# To give an example for accessing variables from a class.
my $variable = "WHATEVER";
sub get_variable { return $variable;}

1;

Then to test your code:

perl -e "use child; $object = child->new(); $object->parent_or_child_function();"

or to script it up properly;

# Load up child class
use child qw();

# Invoke constructor to create an instance of the class
my $object = child->new();

# Invoke function from child class
$object->parent_or_child_function();

# Get Variable
$object->get_variable();
2
  • Thanks you. This explains it quite comprehensively. It leads me to another question, though - what if I have a variable declared within a package? Can I access this variable through a class (package) object?
    – Bob
    Aug 15, 2014 at 3:21
  • Apologies, just returned from a 4 day vacation. You can always access any variable from the class. Just write up an public accessor for it, which returns that variable. (See amended example)
    – rurouni88
    Aug 20, 2014 at 6:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.