From: sln on
On Sat, 15 May 2010 05:17:47 -0700 (PDT), Dilbert <dilbert1999(a)gmail.com> wrote:

>I have been using swig http://www.swig.org/ to incorporate simple C
>subroutines into my Perl programs. That works fine, no complaint at
>all.
>
>However, I would now like to get my hands dirty and learn how to
>communicate between Perl and C from the bottom up using XS-code.
>
>I am thinking about reading "perldoc perlxs", but before I embark on
>that adventure, I would ask for your oppinion:
>
> - Would you recommed reading "perldoc perlxs" ?
> - Are there any other tutorials ?
>
>For example, if I wrote a C-function
>
> first_test(int i, double d, char* s)
>
>that returns a list -- three values:
>
> * the first value is i - 1234
> * the second value is d / 4.2
> * the third value is a string "<".s.">".
>
>My first beginner questions would be like:
>
> - How do parameters get passed into the C-function ?
> - What happens if Perl gives a string, but the C-function expects a
>double ?
> - What happens if Perl gives a double, but the C-function expects a
>string ?
> - How can a C-function return a list of values ?
> - Do I need to allocate (malloc ?) memory to return values ?
> - Who is reponsible for garbage collections of the allocated memory ?
> - Can I pass / return more complicated data structures (hashes of
>hashes, lists of lists, etc...) ?
>
>Many questions, but I don't expect an immediate answer. What I really
>want to know is: what would be the best tutorial for me ?

I would imagine Perl is written in C, where there is no stateless types.
Every thing is a strict type: ptr, int (32/16/8), char, struct, enum,union and
(...), etc. This is done to partition (format) memory, stack or otherwise.
Passing/returning data/value to/from functions follow ANSI C standards.
Some of your questions are on this level. For the answers to these questions,
you should learn C before you attempt xs interface.

-sln