1

I am writing XS module. And want to reuse functions from another XS in Sub::Indentify.

In case of usual perl module I can do:

use Sub::Identify;
Sub::Identify::get_code_info( $code );

How do same thing from XS?

UPD
I want to call get_code_info as C function not as perl subroutine

Eugen Konkov
  • 22,193
  • 17
  • 108
  • 158
  • 1
    `get_code_info` isn't a C function. Calling Perl subs is documented in perlcall. You basically have to do the opposite of XS; write a wrapper that glues a Perl sub to C. – ikegami Jan 18 '18 at 15:52
  • @ikegami: I understand now. the `get_code_inf` is defined after `MODULE` so it is perl sub. Is there a way to reuse functions which are defined before `MODULE` (those which are not perl sub)? – Eugen Konkov Jan 18 '18 at 15:55
  • Yes. Those are just ordinary C functions. Nothing special needs to be done. – ikegami Jan 18 '18 at 15:56
  • 1
    Of course, if you want to do that from another .c or .xs, that gets a bit complicated because that requires linking (statically or dynamically) to the products of the .xs file. You should instead created a separate .c (and .h) that creates the function you want multiple other .c to use, and link to that object. – ikegami Jan 18 '18 at 16:02
  • @ikegami: Just for example. How it would look if I try to reuse [this function](https://metacpan.org/source/PEVANS/Syntax-Keyword-Try-0.09/lib/Syntax/Keyword/Try.xs#L369) from my `.xs`: 1. How make visible that `.xs` to mine; 2. How to link to that object? – Eugen Konkov Jan 18 '18 at 16:04
  • 1
    From a different `xs`? 1. For starters, remove the `static` keyword. That scopes it to the file. It might not even end up in the object file (.obj/.o) if nothing reference it. 2. Now that it's exposed, you probably rename it to avoid conflicts. Remember, C doesn't have namespaces. 3. You'd have to have the DLL (.dll or .so) that .xs generated handy. Use your system's DLL loader to load the DLL and get a pointer to the sub. /// Again, this is very very backwards. Make a library from the sub and have both .xs link to that library. – ikegami Jan 18 '18 at 16:16
  • ...plenty of modules link to external libraries. Use any of them as an example. – ikegami Jan 18 '18 at 17:41
  • @ikegami: Yes, link to external libraries, but not perl .xs. Those (from `Devel`) I see copy/paste C functions and state it is not possible... ( – Eugen Konkov Jan 18 '18 at 17:44
  • Your comment makes no sense. We're not talking about a .xs file. We're talking about a DLL. – ikegami Jan 18 '18 at 18:25
  • I suppose most other modules link to a static library or object that has stubs that load the actual function from the DLL, and you won't have that for an XS file. So you'll have to load the DLL yourself. In a way, that's easier because you don't have to do anything will building your .xs file; it will done at run-time. The downside is lack of portability since DLL-loading functions vary by OS. – ikegami Jan 18 '18 at 18:32
  • https://www.dwheeler.com/program-library/Program-Library-HOWTO/x172.html – ikegami Jan 18 '18 at 18:37

0 Answers0