CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
Search Posts SEARCH
Who's Online WHO'S
Log in LOG

Home: Perl Programming Help: Intermediate:
perl and OOP


New User

Aug 12, 2006, 5:44 PM

Post #1 of 2 (274 views)
perl and OOP Can't Post

I want to make sure if perl have overload feature when a class inherated from other class.Thanks

Thaumaturge / Moderator

Aug 14, 2006, 5:07 AM

Post #2 of 2 (266 views)
Re: [torperl] perl and OOP [In reply to] Can't Post

I think you mean "override", not "overload". In which case, I think the following code demonstrates what you want:

use strict; 
use warnings;

package ClassOne;

sub new {
return bless {}, shift;

sub method1 {
return "this is method1\n";

sub method2 {
return "this is method2\n";

package ClassTwo;

our @ISA = 'ClassOne';

sub method2 {
return "this is method2A\n";

package main;

my $c1 = ClassOne->new;
my $c2 = ClassTwo->new;

print ref($c1) . ':' . $c1->method1;
print ref($c1) . ':' . $c1->method2;
print ref($c2) . ':' . $c2->method1;
print ref($c2) . ':' . $c2->method2;

ClassTwo is a subclass of ClassOne. It inherits "new" and "method1", but overrides "method2".

Dave Cross, Perl Hacker, Trainer and Writer
Get more help at Perl Monks


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives