![]() |
perl polymorphic behavoir ?
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(); |
Re: perl polymorphic behavoir ?
"surf" <surfunbear@yahoo.com> wrote:
> 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 -- -------------------- http://NewsReader.Com/ -------------------- Usenet Newsgroup Service $9.95/Month 30GB |
Re: perl polymorphic behavoir ?
surf <surfunbear@yahoo.com> schrob:
> 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; |
Re: perl polymorphic behavoir ?
"surf" <surfunbear@yahoo.com> wrote in news:1139330403.956594.253620
@g44g2000cwa.googlegroups.com: > 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 -- -- A. Sinan Unur <1usa@llenroc.ude.invalid> (reverse each component and remove .invalid for email address) comp.lang.perl.misc guidelines on the WWW: http://mail.augustmail.com/~tadmc/cl...uidelines.html |
| All times are GMT. The time now is 06:24 AM. |
Powered by vBulletin®. Copyright ©2000 - 2013, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.