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

Hello monks,

I had been trying to pass a complex structure as an argument for a C function. I am trying to do it in XSUB. But I am unable to do so. I have read a number of tutorials, perldocs, ebooks but I am unable to find the solution. I tried to go through the C code generated by xsubpp, so that I can make out a way to do it. But I was unable to understand few things mentioned below:

XSprePUSH (i guess, to generated space in stack like EXTEND(sp, n) macro

dVAR (defined as dNOOP)

dXSARGS (I read about it in perldoc, but I feel that we can easily code without using this. so what is the real purpose of this macro? )

dXSTARG

Is it possible to understand the C code generated and write our own C code without using xsubpp/*.xs, so that we can have a in-depth knowledge of the things happening 'under the hood'?

  • Comment on Some guidance required regarding Perl XS

Replies are listed 'Best First'.
Re: Some guidance required regarding Perl XS
by Corion (Patriarch) on Sep 06, 2015 at 07:05 UTC

    Of course you can write Perl extensions without XS, but the XS macros encapsulate much function. You can easily have your C compiler expand the macros to the full C code. For example gcc -E will output the expanded C file and there also is expand-macro.pl in the Perl distribution that will expand macros so you can understand them better.

    If you are dealing with complex data structures, I would write code in Perl to convert the complex data structure to a string or a simpler data structure and then pass that to the C function(s). Dealing with complex data structures is usually much easier from Perl than from C.

    For documentation of some macros, see perlxs.