[Chicago-talk] default methods

JT Smith jt at plainblack.com
Mon Jul 26 18:30:50 CDT 2004


Thanks dude. Don't know how I've been using perl for 8 years without knowing that. 

I suck. =(

On Mon, 26 Jul 2004 17:02:29 -0500
  Jim Thomason <thomasoniii at gmail.com> wrote:
>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
>>
>_______________________________________________
>Chicago-talk mailing list
>Chicago-talk at mail.pm.org
>http://mail.pm.org/mailman/listinfo/chicago-talk


JT ~ Plain Black

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



More information about the Chicago-talk mailing list