Chapter 8
To call this subroutine from C, I'd have to do the following:
dSP;                                             // declare SP
PUSHMARK(SP);                                    // setup for call
call_pv("Hello::say_hello", G_NOARGS|G_DISCARD); // call say_hello with no args
                                                 // and no return value
The dSP macro is necessary to declare the SP variable used by the PUSHMARK macro. 
These two macros set up the correct context for a call to call_pv(). The first 
argument is a string giving the name of the subroutine. The second specifies 
options for the call. In this case, combining  G_NOARGS and G_DISCARD with | specifies 
a call with no arguments and no return value.
Example 2: One Parameter, No Return Value
A slightly more complicated call is one that passes a parameter. For example, let's 
say I modified say_hello to take an argument:
sub say_hello {
   my $who = shift;
   print "Hello, $who.\n";
}
This code would be required to call it from C:
dSP;                                    // declare SP
ENTER; SAVETMPS;                        // start a new scope
PUSHMARK(SP) ;                          // prepare to push args
XPUSHs(sv_2mortal(newSVpv("Human",0))); // push a single parameter onto the
                                        // argument stack
PUTBACK;                                // done pushing arguments
call_pv("Hello::say_hello", G_DISCARD); // make the call
FREETMPS; LEAVE;                        // end the scope   freeing mortal
                                        // variables
There are a few new things to notice here. First, this code creates a new scope using 
the ENTER, SAVETMPS, FREETMPS, and LEAVE macros you saw back in the  Memory 
Management  section. This is done to provide for mortal variables created for use 
as parameters as well as those created by the Perl code to be called. Second, a single 
198






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