[Chicago-talk] default methods

Jim Thomason thomasoniii at gmail.com
Mon Jul 26 17:02:29 CDT 2004


AUTOLOAD.

package MyClass;

sub AUTOLOAD {
  goto &defaultHandler;
}

You could actually just call defaultHandler as a method or whatnot,
but the goto is actually better in this case.

-Jim...

On Mon, 26 Jul 2004 16:08:07 -0500, JT Smith <jt at plainblack.com> wrote:
> 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.
> _______________________________________________
> Chicago-talk mailing list
> Chicago-talk at mail.pm.org
> http://mail.pm.org/mailman/listinfo/chicago-talk
>



More information about the Chicago-talk mailing list