If you want to wrap a library function (let's call it 'foo') in an XS sub, and be able to call that XS sub from perl as 'foo', then you make use of the XS PREFIX option - which allows you to specify a prefix that will be automatically stripped from the XS sub when it is bound to perl.

For example, specify PREFIX as 'wrap_this_', write the XS Sub as:
int wrap_this_foo(int x) { return foo(x); }
Then, when you call foo(16) in perl, wrap_this_foo(16) is executed.

Here's an Inline::C demo that gets a perl function named 'foo' to execute a C function named 'foo', via an XS sub named 'wrap_this_foo' that wraps the C sub 'foo'.
use strict; use warnings; use Inline C => Config => PREFIX => 'wrap_this_', BUILD_NOISY => 1, ; use Inline C => <<'END_C'; typedef int unbind; /* For this demo, we don't want foo * to bind to perl */ unbind foo(int x) { return x * 2; } int wrap_this_foo(int x){ return foo(x); } END_C print foo(16); # Outputs 32
If you comment out the the wrap_this_foo XS sub and re-run the script then you get:
Undefined subroutine &main::foo called at try.pl line 25.
I hope that proves that the original run was, in fact, calling wrap_this_foo and not directly calling the C function foo (which is invisible to perl).
Update: It's invisible to perl because perl's typemap doesn't explain how to deal with the type 'unbind'.

Cheers,
Rob

In reply to Re^3: Accessing an XS function from a Perl function of the same name by syphilis
in thread Accessing an XS function from a Perl function of the same name by stevieb

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.