skywalker has asked for the wisdom of the Perl Monks concerning the following question:

Hi,

I have a number of object class's that work fine, however I am attempting to build a class that will inherite methods from its children, I have tried including the required methods in the ISA array, however the compiler keeps informing me that it 'cant locate object method method_name' in class test'

I am using the following code
use lib ('G:\Perl Live\OO Packages'); use Mailsort; use strict; my $file = 'J:\sample file.csv'; my $ob = new Mailsort($file); #unable to call this method which is in class Mailsort_scj $ob->update_database('DSA','a database'); print $ob->{DATABASE}."\n";

the header from package Mailsort is as
package Mailsort; use lib ('G:\Perl Live\OO Packages'); require Exporter; require Mailsort_scj; require Filedetails; use strict; use Carp; my @ISA = qw(Exporter Filedetails Mailsort_scj); sub new{ my ($proto,$type) =@_; my $typein = ref($type) || $type; my $params = new Mailsort_scj($typein); &initialise($params); bless ($params,$typein); return($params); }
the header from class Mailsort_scj is as follows;
package Mailsort_scj; require Exporter; require Filedetails; use strict; use Carp; my @ISA = qw(Exporter Filedetails Mailsort_scj); my @EXPORT = qw(update_database update_options update_tntoptions update_inputformat create_scj update_Foreign_Addresses Redirect_Rejects); sub new{ my ($proto,$filein) = @_; my $type = ref($proto) || $proto; my $params = new Filedetails; $params->{FileName_IN} = $filein; &initialise($params); bless $params,$type; return $params; } sub update_database{ my ($class,$type,$value) = @_; my $valuein = ref($value) || $value; $class->{Database}{$type} = $valuein; }

any pointers would be greatly appreciated; thanks Skywalker

Replies are listed 'Best First'.
Re: how to method Inheritance
by chromatic (Archbishop) on May 06, 2009 at 18:06 UTC
    my @ISA = qw(Exporter Filedetails Mailsort_scj); my @EXPORT = qw(update_database update_options update_tntoptions update_inputformat create_scj update_Foreign_Addresses Redirect_Rejects);

    These variables can't be lexical; they must be package global variables. Declare them with our or the vars pragma. Alternately, use the base or parent module to inherit from a superclass.

      Fantastic, thanks for the help

      using the base pragma, and removing the require statement worked a treat

      package Mailsort; use lib ('G:\Perl Live\OO Packages'); require Exporter; use strict; use Carp; use base qw(Mailsort_scj Filedetails);
      however why does base work here where require did not?

      thanks

        RTFM: base (and parent) modify @ISA.

        Alexander

        --
        Today I will gladly share my knowledge and experience, for there are no sweeter words than "I told you so". ;-)
Re: how to method Inheritance
by ikegami (Patriarch) on May 06, 2009 at 16:00 UTC

    You made an object of class Mailsort, but then you turned around and told Perl the object isn't a Mailsort object but rather a DSA object (bless ($params,$typein);). Take your pick.