[Chicago-talk] default methods

JT Smith jt at plainblack.com
Mon Jul 26 16:08:07 CDT 2004


I need some help from the perl experts. Any and all advice would be much appreciated.


Consider that I have a class that looks like this:


package MyClass;

use strict;

sub new {
   my $class = shift;
   my $something = 1;
   bless {something=>$something}, $class;
}

sub someMethod {
   return "foo";
}

sub defaultHandler {
   return "no such method";
}

1;


Now let's say that a user creates a program like this:

#!/usr/bin/perl

use strict;
use MyClass;

my $obj = MyClass->new;
print $obj->someMethod."\n";
print $obj->someOtherMethod."\n";



The program will crap out of course because someOtherMethod doesn't exist. My question 
is, is there a way to get perl to call defaultHandler if a method is called that does 
not exist?


JT ~ Plain Black

Create like a god, command like a king, work like a slave. 



More information about the Chicago-talk mailing list