philou has asked for the wisdom of the Perl Monks concerning the following question:

Hi all, I am writing an XS module, and calling Perl subs from C. I come to a point where I need to check for existence of a Perl sub ( given its name ), I get into trouble with get_cv ( perldoc perlapi ), which does not seem to handle each and every case. Here are the .xs and the test.pl file I use:
CCC.xs :
#include "EXTERN.h" #include "perl.h" #include "XSUB.h" void inspect( char* name ) { if( get_cv( name, (I32)0 ) != NULL ) { printf( " &%s is defined\n", name ); { dSP ; PUSHMARK(SP) ; call_pv( name, G_DISCARD | G_NOARGS ); } } } MODULE = CCC PACKAGE = CCC void inspect( name ) INPUT: char* name;
test.pl :
sub mysub { print "Hello from mysub !\n"; } &CCC::inspect( "main::mysub" ); undef &mysub; &CCC::inspect( "main::mysub" );
When I run `make test', I get the following output :
1..1 ok 1 &main::mysub is defined Hello from mysub ! &main::mysub is defined Undefined subroutine &main::mysub called at test.pl line 27. make: *** [test_dynamic] Erreur 255
So the first time it gets called, it works fine, but after undefining the sub, it does not detect the sub is undef and calls it. Note that the perlapi says that with get_cv you get NULL if "create" is not set and the subroutine does not exist. Is there a way to check for sub definedness rather then for sub existence ? Cheers, Philou

Replies are listed 'Best First'.
Re: How to check for subroutine existence from within XS code
by Elian (Parson) on May 23, 2003 at 18:24 UTC
    Well, this isn't too surprising--undef doesn't obliterates something from existence, it merely removes its contents. The error message is correct, and what ought to happen. Arguably you'd not want to change that behavior, since if there was an AUTOLOAD in the hierarchy it would get called However...

    If you really do want to catch subs that exist but aren't defined and don't care about AUTOLOAD, look at the cv you get back. It'll either be undef or not SvOK. Possibly it won't be of SvTYPE SVt_PVCV, but I'm not 100% sure there.

Re: How to check for subroutine existence from within XS code
by dug (Chaplain) on May 23, 2003 at 18:11 UTC
    The following patch makes the tests pass, but please be cautioned that my XS-foo is *very* limited. The CvXSUB CvROOT stuff is stolen directly from dump.c of the 5.8.0 source, specifically the Perl_dump_sub routine at line 70.

    Let me re-iterate. I'm no expert here. This just worked for me.
    --- CCC.xs~ 2003-05-23 13:58:57.000000000 -0400 +++ CCC.xs 2003-05-23 13:58:46.000000000 -0400 @@ -2,15 +2,18 @@ #include "perl.h" #include "XSUB.h" - void inspect( char* name ) { - if( get_cv( name, (I32)0 ) != NULL ) { - printf( " &%s is defined\n", name ); + GV *glob = CvGV(get_cv( name, (I32)0 )); + if ( (CvXSUB(GvCV(glob))) || CvROOT(GvCV(glob)) ) { + printf( " &%s has code\n", name ); { dSP ; PUSHMARK(SP) ; call_pv( name, G_DISCARD | G_NOARGS ); } + } + else { + printf( " &%s is undefined\n", name ); } }
    -- dug