perl polymorphic behavoir ?

S

surf

I'm new at trying OO in perl and somewhat rusty on OO in general, but
is there a limitation here ? Below I call foo2() from a subclass.
foo2() in the base class calls foo3(). Since my instance is the
subclass I think it should call the subclass foo3() but it calls the
base class. If there isn't a way to do this, I would think perl is
lacking
in OO features, am I wrong ?





package A;

sub foo2 {

print("Inside A::foo\n");
foo3();
}


sub foo3
{
print("foo3 A\n");
}

package B;

@ISA = (A);


sub foo3
{
print("foo3 B\n");
}

sub foo {

print("Inside B::foo\n");

}



package main;

B->foo2();
 
X

xhoster

surf said:
I'm new at trying OO in perl and somewhat rusty on OO in general, but
is there a limitation here ?

You are not using OO in perl. Or at least you are not doing so at the
point where the problem occurs.
Below I call foo2() from a subclass.
foo2() in the base class calls foo3(). Since my instance is the

What instance? You are not using an instance.
subclass I think it should call the subclass foo3() but it calls the
base class. If there isn't a way to do this, I would think perl is
lacking
in OO features, am I wrong ?

package A;

sub foo2 {

print("Inside A::foo\n");
foo3();

This is just a plain old subroutine call. It is not a method call.
A method call would look something like:

$_[0]->foo3(); # usually $self->foo3(), but since you don't create $self...


Xho
 
L

Lukas Mai

surf said:
I'm new at trying OO in perl and somewhat rusty on OO in general, but
is there a limitation here ? Below I call foo2() from a subclass.
foo2() in the base class calls foo3(). Since my instance is the
subclass I think it should call the subclass foo3() but it calls the
base class. If there isn't a way to do this, I would think perl is
lacking
in OO features, am I wrong ?

(note: code rearranged)

Missing:
use warnings;
use strict;
package B;

@ISA = (A);
^ You forgot to quote this string.
use strict would have told you that.
^ @ISA is implicitly global.
use strict would have told you that.
sub foo3
{
print("foo3 B\n");
}

sub foo {

print("Inside B::foo\n");

}

package main;

B->foo2();

This is a class method call. B::foo2 doesn't exist, so it follows @ISA
and calls A::foo2 (as &A::foo2('B')).
package A;

sub foo3
{
print("foo3 A\n");
}
sub foo2 {

print("Inside A::foo\n");
foo3();
}

This is an ordinary function call. We're in package A, so it calls
A::foo3(), period. If you want method call semantics, you need to use ->
(as in INVOCANT->METHOD()). If you want to use the same class that was
passed in, er ... just do it:

$_[0]->foo3();

It's more readable to use an extra variable, though:

my $class = shift;
$class->foo3;
 
A

A. Sinan Unur

I'm new at trying OO in perl and somewhat rusty on OO in general, but
is there a limitation here ? Below I call foo2() from a subclass.
foo2() in the base class calls foo3(). Since my instance is the
subclass I think it should call the subclass foo3()

You are not calling an instance method. You are calling a class method.
But that has nothing to do with the problem.

but it calls the
base class. If there isn't a way to do this, I would think perl is
lacking in OO features, am I wrong ?

You are correct. You cannot do OO in Perl. You should switch to PHP.

use strict;
use warnings;

missing.
package A;

sub foo2 {

print("Inside A::foo\n");
foo3();
}


sub foo3
{
print("foo3 A\n");
}

package B;

@ISA = (A);


sub foo3
{
print("foo3 B\n");
}

sub foo {

print("Inside B::foo\n");

}



package main;

B->foo2();

Your subs are not methods. A method pays attention to the first argument
in the invocation.

You can figure out the rest.

I have yet to see from you an attempt to put some time and effort into
your questions. You do not follow the posting guidelines, and your code
is awfully formatted.

Sinan


--
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,776
Messages
2,569,603
Members
45,189
Latest member
CryptoTaxSoftware

Latest Threads

Top