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

In reply to How to check for subroutine existence from within XS code by philou

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.