Programming Perl in C
argument is pushed onto the argument stack using the XPUSHs macro. Next, the 
PUTBACK macro is used to mark that the last of the arguments has been pushed onto 
the stack. Finally, call_pv() is used, just as in the first example, but this time only 
G_DISCARD is used as an option.
Example 3: Variable Parameters, One Return Value
For a slightly more realistic example, let's call the Data::Counter::count function 
defined a couple of chapters back. If you remember, this subroutine takes a 
variable number of arguments and returns the number of arguments it received:
sub count { return scalar @_; }
To call this subroutine, I need the following code:
dSP;                                    // declare SP
SV *return                              // declare an SV for the return value
ENTER; SAVETMPS;                        // start a new scope
PUSHMARK(SP) ;                          // prepare to push args
XPUSHs(sv_2mortal(newSVpv("one",0)));   // push three parameters onto the stack
XPUSHs(sv_2mortal(newSVpv("two",0)));
XPUSHs(sv_2mortal(newSVpv("three",0)));
PUTBACK;                                // done pushing arguments
call_pv("Data::Counter::count", G_SCALAR); // make the call
SPAGAIN;                                // refresh SP   it might have changed
return = POPs;                          // get return value off the stack
printf("count: %d\n", SvIV(return));    // print out the return as an integer
FREETMPS; LEAVE;                        // end the scope   freeing mortal
                                        // variables
This code is similar to the last example with a couple additions. First, I passed 
G_SCALAR as an option to call_pv(), indicating that I expect to get a single argument 
back. After the call, a new macro is used: SPAGAIN. This macro refreshes SP in case it 
changed during the call. Finally, the return value from the subroutine is popped off 
the stack with POPs. The  s  in POPs refers to the fact that it pops a scalar off the stack.
19
199
9






footer




 

 

 

 

 Home | About Us | Network | Services | Support | FAQ | Control Panel | Order Online | Sitemap | Contact

web hosting perl

 

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