Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

basic object-oriented Perl question

Status
Not open for further replies.

DoraC

Programmer
May 7, 2002
98
US
hi

I can't seem to figure out how to properly implement inheritance. I have one base class that I would like a variety of other classes to inherit from - but every time I try to code this (following my books or internet examples) I run into problems. I have the base class defined in one module (base.pm), and the children-to-be in others (say, child.pm). These children are then instantiated in another Perl program.

examples:

Code:
#***in file base.pm
package base  

sub new
{
#-- new code here
}

sub bprint()
{
#-- some print code here
}


#***in file child.pm
package child 

use base;
@ISA = qw(base); #so we're a base, and those methods should be 
                 #available to instances of child

sub new
{
#-- new code here
}

sub print
{
#-- some other code here
}

#***in file main.p
use child;

$var = new child; #so far, so good

$var->print(); #still ok

$var->bprint(); #not ok, I get an error stating:
"can't locate object method "bprint" via package "child"

I first tried to access "bprint" from within the child module itself, and then moved it out to the main.p when that didn't work. I thought that the @ISA array would be searched for a package containing a method not defined in the current package, and hence "bprint" would be found? It doesn't seem to recognize that what's in base.pm is a package - I can do whatever I want to that code and not affect the erroneous outcome.

hmmmm, any help greatly appreciated (i've done lots of experimenting, to no avail..)
[sad]
dora c
 
Code:
#=========== FILE:  testbase.pm ========
package testbase;

sub base_p {
print "Found the base_p function\n";
}

1;
#=======================================

#========== FILE:  testchild.pm ========
package testchild;
use testbase;
@ISA = qw(testbase);

sub child_p {
print "Found the child_p function\n";
}

1;
#=======================================
After creating those two files, I was able to execute this code:

Code:
use testchild;

$j = new testchild;

$j->child_p;
$j->base_p;

And the output is what would be expected:

Found the child_p function
Found the base_p function


Do you see a difference in our code that may prevent yours from functioning?

--jim
 
is base a reserved word? Mike
________________________________________________________________________________

"Experience is the comb that Nature gives us, after we are bald."

Is that a haiku?
I never could get the hang
of writing those things.
 

"base" a reserved word?
I replaced "base" with "testbase"
and now my code works

[wink]
dora
 
hi Coderifous,

when i try your code. and run the program i get this error


Can't locate object method "new" via package "testchild" at newpr.pl line 3.


thanks in advance
19decsat
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top