in reply to Re: Re: Perl extension parameters (by ref)
in thread Perl extension parameters

Arguments to Perl subroutines are passed by alias. That is a lot like 'pass by reference' but not quite exactly and the word 'reference' in 'pass by reference' is only a bit related to the word 'reference' in 'Perl reference'.

The "OUTPUT:" tells the XS code to do the equivalent of $_[0] = .... No need to pass in a reference. For regular Perl subroutines, it is often a good idea to use references rather than relying on 'pass by alias' (it often leads to clearer code), but for XS subroutines it would mostly add complication and more places to introduce bugs.

A Perl reference wouldn't translate into a C pointer, at least not an 'int *'. It'd give you a pointer to a perl struct (RV) that contained the reference and that you could use to get a pointer to another perl struct (SV) that contains the Perl scalar value. But your current code already gives you this SV* directly. With it, you can copy out an integer value or copy in a new integer value.

The XS system generates code to copy the integer value out of the scalar and into your C variable, iReturn (which then gets passed to the C subroutine). Since you specified "OUTPUT: iReturn", code is also generated to copy the value from iReturn back into the scalar.

Note that the scalar might not have originally contained an integer value (IV) so the first step above might prompt a conversion from the scalar's string or floating-point value into an integer (which gets cached in the SV along with any other types of values that the scalar already has).

When the new integer value gets copied back into the scalar, then any values of other types cached in the scalar are discarded.

- tye