Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Extending an Object in Perl

Tags:

object

perl

I've got a base object called RuleObject and an object that inherits from that called RuleObjectString. I have a new method in RuleObjectString that I want to call in my code that uses that object. But I get the error. 'Can't locate object method "compare" via package "RuleObject" at ./testobject.pl line 10.' But I'm not creating a RuleObject. I'm creating a RuleObjectString. What am I doing wrong here?

testobject.pl

  1 #! /usr/bin/perl
  2
  3 use strict;
  4
  5 use RuleObjectString;
  6
  7 my $s = RuleObjectString->new();
  8 $s->value('stuff goes here');
  9
 10 if ($s->compare('stuff')){
 11         print "MATCH!\n";
 12 }else{
 13         print "no match :(\n";
 14 }

RuleObject.pm

package RuleObject;

our @ISA = qw/Exporter/;
our @EXPORT = qw/new/;

use strict;

sub new{
        my $class = shift;

        my $self;
        $self->{value} = undef;

        bless $self;
        return $self;
}

sub value{
        my $self = shift;
        my $value = shift;
        if ($value){
                $self->{value} = $value;
        }else{
                return $self->{value};
        }
}

RuleObjectString.pm

package RuleObjectString;

our @ISA = qw/RuleObject/;
our @EXPORT = qw/compare/;

use strict;

sub compare{
        my $self = shift;
        my $compareto = shift;

        return $self->value() =~ /$compareto/;
}
like image 930
Michael Avatar asked Nov 16 '12 18:11

Michael


People also ask

What is object method in Perl?

An object within Perl is merely a reference to a data type that knows what class it belongs to. The object is stored as a reference in a scalar variable. Because a scalar only contains a reference to the object, the same scalar can hold different objects in different classes.

How do I inherit in Perl?

Inheritance in Perl can be implemented with the use of packages. Packages are used to create a parent class which can be used in the derived classes to inherit the functionalities. 1; The above code is the definition of the base class.

Does Perl support Object-Oriented Programming?

Summary: in this tutorial, you will learn about Perl Object-Oriented Programming or Perl OOP. You will learn how to create a simple Perl class and use it in other programs. Besides procedural programming, Perl also provides you with object-orient programming paradigm.

What is constructor in Perl?

The constructor method is a Perl subroutine that returns an object which is an instance of the class. It is convention to name the constructor method 'new', but it can be any valid subroutine name. The constructor method works by using the bless function on a hash reference and the class name (the package name).


2 Answers

I think jmcneirney is on the right track. In your RuleObject constructor, you say

bless $self;

which is the same as

bless $self, __PACKAGE__;

or

bless $self, 'RuleObject'

but what you want is for the object to blessed as a RuleObjectString. So what you want to do is say

bless $self, $class

Now

RuleObject->new()
RuleObjectString->new()

will both call the same constructor, but the object returned by the first call will be blessed as a RuleObject and the second object will be blessed as a RuleObjectString.

like image 95
mob Avatar answered Oct 17 '22 15:10

mob


This is 2012, so you should consider using proper OOP solutions instead of reinventing the wheel all over again.

By using Moose, the solution would look something like this (untested):

RuleObject.pm

package RuleObject;
use Moose;

has 'value' => ( isa => 'Str', is => 'rw', required => 0, default => '' );

1;

RuleObjectString.pm

package RuleObjectString;
use Moose;

extends 'RuleObject';

sub compare {
    my $self      = shift;
    my $compareto = shift;

    return $self->value =~ /$compareto/;
}

1;

Simple! :)

like image 30
toreau Avatar answered Oct 17 '22 15:10

toreau