* Tassilo von Parseval wrote:
>On Mon, Aug 23, 2004 at 08:14:45AM +0200 Bjoern Hoehrmann wrote:
>
>>   Should my XS code check whether an object 'can' a specific method
>> before calling call_method() for a reason other than performance? It
>> seems that such a check is not commonly performed so it is probably
>> not very important. 
>
>Depends. When writing pure Perl code, do you usually check that a method
>exists before you call it? I'd say no. However, sometimes you may want
>to do it. The same is true for XS. If it's crucial not to trigger a
>non-existent method, then do a preliminary check.

Right, I looked at it in the context of optional event handlers where
it is common that specific methods do not exist. In Perl, if $o->x is
called even though there is no method x, Perl would die; so my question
is rather whether call_method for a non-existant method has negative
side-effects.

>> What's the best way to check whether an object 'can' a specific
>> method?
>
>You can use 'gv_fetchmethod_autoload':
>
>    /* assuming 'obj' holds the object reference */
>    HV *stash = HvSTASH((SV*)SvRV(obj));
>    
>    if (!gv_fetchmethod_autoload(stash, "method", FALSE))
>       croak("'method' does not exist");
>    else
>       /* call it */
>
>If you want to allow 'method' to be autoloaded, set the third parameter
>to TRUE.

Thanks!

Reply via email to