Chapter 8
NOTE The return value from a Perl subroutine obtained by POPs is a
mortal variable. This means you must handle it before you end the
scope with FREETMPS and LEAVE. After that point the variable will have
been freed by the garbage collector and will no longer be accessible.
Example 4: Variable Parameters, Variable Return Values
In order to demonstrate multiple value returns, let's suppose that count() were
modified to take a list of array refs as a parameter and returns a list of counts for
each array:
sub count { map { scalar @$_ } @_ }
Now count() would be called like this from Perl:
@counts = Data::Counter::count([1,2,3],[4,5,6],[7,8,9]); # returns (3,3,3)
Here's what the equivalent call would look like from C, assuming I already
have the preceding three arrays in the variables av1, av2, and av3.
dSP; // declare SP
int num; // declare an int for the return count
int i; // declare a loop counter
ENTER; SAVETMPS; // start a new scope
PUSHMARK(SP) ; // prepare to push args
XPUSHs(sv_2mortal(newRV_inc((SV*)av1))); // push three arrays onto the stack
XPUSHs(sv_2mortal(newRV_inc((SV*)av2))); // by reference
XPUSHs(sv_2mortal(newRV_inc((SV*)av3)));
PUTBACK; // done pushing arguments
num = call_pv("Data::Counter::count", G_ARRAY); // make the call
SPAGAIN; // refresh SP it might have changed
200
footer
Our partners:
PHP: Hypertext Preprocessor Best Web Hosting
Java Web Hosting
Inexpensive Web Hosting
Jsp Web Hosting
Cheapest Web Hosting
Jsp Hosting
Cheap Hosting
Visionwebhosting.net Business web hosting division of Web
Design Plus. All rights reserved