1*0Sstevel@tonic-gate=head1 NAME 2*0Sstevel@tonic-gate 3*0Sstevel@tonic-gateperlcall - Perl calling conventions from C 4*0Sstevel@tonic-gate 5*0Sstevel@tonic-gate=head1 DESCRIPTION 6*0Sstevel@tonic-gate 7*0Sstevel@tonic-gateThe purpose of this document is to show you how to call Perl subroutines 8*0Sstevel@tonic-gatedirectly from C, i.e., how to write I<callbacks>. 9*0Sstevel@tonic-gate 10*0Sstevel@tonic-gateApart from discussing the C interface provided by Perl for writing 11*0Sstevel@tonic-gatecallbacks the document uses a series of examples to show how the 12*0Sstevel@tonic-gateinterface actually works in practice. In addition some techniques for 13*0Sstevel@tonic-gatecoding callbacks are covered. 14*0Sstevel@tonic-gate 15*0Sstevel@tonic-gateExamples where callbacks are necessary include 16*0Sstevel@tonic-gate 17*0Sstevel@tonic-gate=over 5 18*0Sstevel@tonic-gate 19*0Sstevel@tonic-gate=item * An Error Handler 20*0Sstevel@tonic-gate 21*0Sstevel@tonic-gateYou have created an XSUB interface to an application's C API. 22*0Sstevel@tonic-gate 23*0Sstevel@tonic-gateA fairly common feature in applications is to allow you to define a C 24*0Sstevel@tonic-gatefunction that will be called whenever something nasty occurs. What we 25*0Sstevel@tonic-gatewould like is to be able to specify a Perl subroutine that will be 26*0Sstevel@tonic-gatecalled instead. 27*0Sstevel@tonic-gate 28*0Sstevel@tonic-gate=item * An Event Driven Program 29*0Sstevel@tonic-gate 30*0Sstevel@tonic-gateThe classic example of where callbacks are used is when writing an 31*0Sstevel@tonic-gateevent driven program like for an X windows application. In this case 32*0Sstevel@tonic-gateyou register functions to be called whenever specific events occur, 33*0Sstevel@tonic-gatee.g., a mouse button is pressed, the cursor moves into a window or a 34*0Sstevel@tonic-gatemenu item is selected. 35*0Sstevel@tonic-gate 36*0Sstevel@tonic-gate=back 37*0Sstevel@tonic-gate 38*0Sstevel@tonic-gateAlthough the techniques described here are applicable when embedding 39*0Sstevel@tonic-gatePerl in a C program, this is not the primary goal of this document. 40*0Sstevel@tonic-gateThere are other details that must be considered and are specific to 41*0Sstevel@tonic-gateembedding Perl. For details on embedding Perl in C refer to 42*0Sstevel@tonic-gateL<perlembed>. 43*0Sstevel@tonic-gate 44*0Sstevel@tonic-gateBefore you launch yourself head first into the rest of this document, 45*0Sstevel@tonic-gateit would be a good idea to have read the following two documents - 46*0Sstevel@tonic-gateL<perlxs> and L<perlguts>. 47*0Sstevel@tonic-gate 48*0Sstevel@tonic-gate=head1 THE CALL_ FUNCTIONS 49*0Sstevel@tonic-gate 50*0Sstevel@tonic-gateAlthough this stuff is easier to explain using examples, you first need 51*0Sstevel@tonic-gatebe aware of a few important definitions. 52*0Sstevel@tonic-gate 53*0Sstevel@tonic-gatePerl has a number of C functions that allow you to call Perl 54*0Sstevel@tonic-gatesubroutines. They are 55*0Sstevel@tonic-gate 56*0Sstevel@tonic-gate I32 call_sv(SV* sv, I32 flags) ; 57*0Sstevel@tonic-gate I32 call_pv(char *subname, I32 flags) ; 58*0Sstevel@tonic-gate I32 call_method(char *methname, I32 flags) ; 59*0Sstevel@tonic-gate I32 call_argv(char *subname, I32 flags, register char **argv) ; 60*0Sstevel@tonic-gate 61*0Sstevel@tonic-gateThe key function is I<call_sv>. All the other functions are 62*0Sstevel@tonic-gatefairly simple wrappers which make it easier to call Perl subroutines in 63*0Sstevel@tonic-gatespecial cases. At the end of the day they will all call I<call_sv> 64*0Sstevel@tonic-gateto invoke the Perl subroutine. 65*0Sstevel@tonic-gate 66*0Sstevel@tonic-gateAll the I<call_*> functions have a C<flags> parameter which is 67*0Sstevel@tonic-gateused to pass a bit mask of options to Perl. This bit mask operates 68*0Sstevel@tonic-gateidentically for each of the functions. The settings available in the 69*0Sstevel@tonic-gatebit mask are discussed in L<FLAG VALUES>. 70*0Sstevel@tonic-gate 71*0Sstevel@tonic-gateEach of the functions will now be discussed in turn. 72*0Sstevel@tonic-gate 73*0Sstevel@tonic-gate=over 5 74*0Sstevel@tonic-gate 75*0Sstevel@tonic-gate=item call_sv 76*0Sstevel@tonic-gate 77*0Sstevel@tonic-gateI<call_sv> takes two parameters, the first, C<sv>, is an SV*. 78*0Sstevel@tonic-gateThis allows you to specify the Perl subroutine to be called either as a 79*0Sstevel@tonic-gateC string (which has first been converted to an SV) or a reference to a 80*0Sstevel@tonic-gatesubroutine. The section, I<Using call_sv>, shows how you can make 81*0Sstevel@tonic-gateuse of I<call_sv>. 82*0Sstevel@tonic-gate 83*0Sstevel@tonic-gate=item call_pv 84*0Sstevel@tonic-gate 85*0Sstevel@tonic-gateThe function, I<call_pv>, is similar to I<call_sv> except it 86*0Sstevel@tonic-gateexpects its first parameter to be a C char* which identifies the Perl 87*0Sstevel@tonic-gatesubroutine you want to call, e.g., C<call_pv("fred", 0)>. If the 88*0Sstevel@tonic-gatesubroutine you want to call is in another package, just include the 89*0Sstevel@tonic-gatepackage name in the string, e.g., C<"pkg::fred">. 90*0Sstevel@tonic-gate 91*0Sstevel@tonic-gate=item call_method 92*0Sstevel@tonic-gate 93*0Sstevel@tonic-gateThe function I<call_method> is used to call a method from a Perl 94*0Sstevel@tonic-gateclass. The parameter C<methname> corresponds to the name of the method 95*0Sstevel@tonic-gateto be called. Note that the class that the method belongs to is passed 96*0Sstevel@tonic-gateon the Perl stack rather than in the parameter list. This class can be 97*0Sstevel@tonic-gateeither the name of the class (for a static method) or a reference to an 98*0Sstevel@tonic-gateobject (for a virtual method). See L<perlobj> for more information on 99*0Sstevel@tonic-gatestatic and virtual methods and L<Using call_method> for an example 100*0Sstevel@tonic-gateof using I<call_method>. 101*0Sstevel@tonic-gate 102*0Sstevel@tonic-gate=item call_argv 103*0Sstevel@tonic-gate 104*0Sstevel@tonic-gateI<call_argv> calls the Perl subroutine specified by the C string 105*0Sstevel@tonic-gatestored in the C<subname> parameter. It also takes the usual C<flags> 106*0Sstevel@tonic-gateparameter. The final parameter, C<argv>, consists of a NULL terminated 107*0Sstevel@tonic-gatelist of C strings to be passed as parameters to the Perl subroutine. 108*0Sstevel@tonic-gateSee I<Using call_argv>. 109*0Sstevel@tonic-gate 110*0Sstevel@tonic-gate=back 111*0Sstevel@tonic-gate 112*0Sstevel@tonic-gateAll the functions return an integer. This is a count of the number of 113*0Sstevel@tonic-gateitems returned by the Perl subroutine. The actual items returned by the 114*0Sstevel@tonic-gatesubroutine are stored on the Perl stack. 115*0Sstevel@tonic-gate 116*0Sstevel@tonic-gateAs a general rule you should I<always> check the return value from 117*0Sstevel@tonic-gatethese functions. Even if you are expecting only a particular number of 118*0Sstevel@tonic-gatevalues to be returned from the Perl subroutine, there is nothing to 119*0Sstevel@tonic-gatestop someone from doing something unexpected--don't say you haven't 120*0Sstevel@tonic-gatebeen warned. 121*0Sstevel@tonic-gate 122*0Sstevel@tonic-gate=head1 FLAG VALUES 123*0Sstevel@tonic-gate 124*0Sstevel@tonic-gateThe C<flags> parameter in all the I<call_*> functions is a bit mask 125*0Sstevel@tonic-gatewhich can consist of any combination of the symbols defined below, 126*0Sstevel@tonic-gateOR'ed together. 127*0Sstevel@tonic-gate 128*0Sstevel@tonic-gate 129*0Sstevel@tonic-gate=head2 G_VOID 130*0Sstevel@tonic-gate 131*0Sstevel@tonic-gateCalls the Perl subroutine in a void context. 132*0Sstevel@tonic-gate 133*0Sstevel@tonic-gateThis flag has 2 effects: 134*0Sstevel@tonic-gate 135*0Sstevel@tonic-gate=over 5 136*0Sstevel@tonic-gate 137*0Sstevel@tonic-gate=item 1. 138*0Sstevel@tonic-gate 139*0Sstevel@tonic-gateIt indicates to the subroutine being called that it is executing in 140*0Sstevel@tonic-gatea void context (if it executes I<wantarray> the result will be the 141*0Sstevel@tonic-gateundefined value). 142*0Sstevel@tonic-gate 143*0Sstevel@tonic-gate=item 2. 144*0Sstevel@tonic-gate 145*0Sstevel@tonic-gateIt ensures that nothing is actually returned from the subroutine. 146*0Sstevel@tonic-gate 147*0Sstevel@tonic-gate=back 148*0Sstevel@tonic-gate 149*0Sstevel@tonic-gateThe value returned by the I<call_*> function indicates how many 150*0Sstevel@tonic-gateitems have been returned by the Perl subroutine - in this case it will 151*0Sstevel@tonic-gatebe 0. 152*0Sstevel@tonic-gate 153*0Sstevel@tonic-gate 154*0Sstevel@tonic-gate=head2 G_SCALAR 155*0Sstevel@tonic-gate 156*0Sstevel@tonic-gateCalls the Perl subroutine in a scalar context. This is the default 157*0Sstevel@tonic-gatecontext flag setting for all the I<call_*> functions. 158*0Sstevel@tonic-gate 159*0Sstevel@tonic-gateThis flag has 2 effects: 160*0Sstevel@tonic-gate 161*0Sstevel@tonic-gate=over 5 162*0Sstevel@tonic-gate 163*0Sstevel@tonic-gate=item 1. 164*0Sstevel@tonic-gate 165*0Sstevel@tonic-gateIt indicates to the subroutine being called that it is executing in a 166*0Sstevel@tonic-gatescalar context (if it executes I<wantarray> the result will be false). 167*0Sstevel@tonic-gate 168*0Sstevel@tonic-gate=item 2. 169*0Sstevel@tonic-gate 170*0Sstevel@tonic-gateIt ensures that only a scalar is actually returned from the subroutine. 171*0Sstevel@tonic-gateThe subroutine can, of course, ignore the I<wantarray> and return a 172*0Sstevel@tonic-gatelist anyway. If so, then only the last element of the list will be 173*0Sstevel@tonic-gatereturned. 174*0Sstevel@tonic-gate 175*0Sstevel@tonic-gate=back 176*0Sstevel@tonic-gate 177*0Sstevel@tonic-gateThe value returned by the I<call_*> function indicates how many 178*0Sstevel@tonic-gateitems have been returned by the Perl subroutine - in this case it will 179*0Sstevel@tonic-gatebe either 0 or 1. 180*0Sstevel@tonic-gate 181*0Sstevel@tonic-gateIf 0, then you have specified the G_DISCARD flag. 182*0Sstevel@tonic-gate 183*0Sstevel@tonic-gateIf 1, then the item actually returned by the Perl subroutine will be 184*0Sstevel@tonic-gatestored on the Perl stack - the section I<Returning a Scalar> shows how 185*0Sstevel@tonic-gateto access this value on the stack. Remember that regardless of how 186*0Sstevel@tonic-gatemany items the Perl subroutine returns, only the last one will be 187*0Sstevel@tonic-gateaccessible from the stack - think of the case where only one value is 188*0Sstevel@tonic-gatereturned as being a list with only one element. Any other items that 189*0Sstevel@tonic-gatewere returned will not exist by the time control returns from the 190*0Sstevel@tonic-gateI<call_*> function. The section I<Returning a list in a scalar 191*0Sstevel@tonic-gatecontext> shows an example of this behavior. 192*0Sstevel@tonic-gate 193*0Sstevel@tonic-gate 194*0Sstevel@tonic-gate=head2 G_ARRAY 195*0Sstevel@tonic-gate 196*0Sstevel@tonic-gateCalls the Perl subroutine in a list context. 197*0Sstevel@tonic-gate 198*0Sstevel@tonic-gateAs with G_SCALAR, this flag has 2 effects: 199*0Sstevel@tonic-gate 200*0Sstevel@tonic-gate=over 5 201*0Sstevel@tonic-gate 202*0Sstevel@tonic-gate=item 1. 203*0Sstevel@tonic-gate 204*0Sstevel@tonic-gateIt indicates to the subroutine being called that it is executing in a 205*0Sstevel@tonic-gatelist context (if it executes I<wantarray> the result will be true). 206*0Sstevel@tonic-gate 207*0Sstevel@tonic-gate 208*0Sstevel@tonic-gate=item 2. 209*0Sstevel@tonic-gate 210*0Sstevel@tonic-gateIt ensures that all items returned from the subroutine will be 211*0Sstevel@tonic-gateaccessible when control returns from the I<call_*> function. 212*0Sstevel@tonic-gate 213*0Sstevel@tonic-gate=back 214*0Sstevel@tonic-gate 215*0Sstevel@tonic-gateThe value returned by the I<call_*> function indicates how many 216*0Sstevel@tonic-gateitems have been returned by the Perl subroutine. 217*0Sstevel@tonic-gate 218*0Sstevel@tonic-gateIf 0, then you have specified the G_DISCARD flag. 219*0Sstevel@tonic-gate 220*0Sstevel@tonic-gateIf not 0, then it will be a count of the number of items returned by 221*0Sstevel@tonic-gatethe subroutine. These items will be stored on the Perl stack. The 222*0Sstevel@tonic-gatesection I<Returning a list of values> gives an example of using the 223*0Sstevel@tonic-gateG_ARRAY flag and the mechanics of accessing the returned items from the 224*0Sstevel@tonic-gatePerl stack. 225*0Sstevel@tonic-gate 226*0Sstevel@tonic-gate=head2 G_DISCARD 227*0Sstevel@tonic-gate 228*0Sstevel@tonic-gateBy default, the I<call_*> functions place the items returned from 229*0Sstevel@tonic-gateby the Perl subroutine on the stack. If you are not interested in 230*0Sstevel@tonic-gatethese items, then setting this flag will make Perl get rid of them 231*0Sstevel@tonic-gateautomatically for you. Note that it is still possible to indicate a 232*0Sstevel@tonic-gatecontext to the Perl subroutine by using either G_SCALAR or G_ARRAY. 233*0Sstevel@tonic-gate 234*0Sstevel@tonic-gateIf you do not set this flag then it is I<very> important that you make 235*0Sstevel@tonic-gatesure that any temporaries (i.e., parameters passed to the Perl 236*0Sstevel@tonic-gatesubroutine and values returned from the subroutine) are disposed of 237*0Sstevel@tonic-gateyourself. The section I<Returning a Scalar> gives details of how to 238*0Sstevel@tonic-gatedispose of these temporaries explicitly and the section I<Using Perl to 239*0Sstevel@tonic-gatedispose of temporaries> discusses the specific circumstances where you 240*0Sstevel@tonic-gatecan ignore the problem and let Perl deal with it for you. 241*0Sstevel@tonic-gate 242*0Sstevel@tonic-gate=head2 G_NOARGS 243*0Sstevel@tonic-gate 244*0Sstevel@tonic-gateWhenever a Perl subroutine is called using one of the I<call_*> 245*0Sstevel@tonic-gatefunctions, it is assumed by default that parameters are to be passed to 246*0Sstevel@tonic-gatethe subroutine. If you are not passing any parameters to the Perl 247*0Sstevel@tonic-gatesubroutine, you can save a bit of time by setting this flag. It has 248*0Sstevel@tonic-gatethe effect of not creating the C<@_> array for the Perl subroutine. 249*0Sstevel@tonic-gate 250*0Sstevel@tonic-gateAlthough the functionality provided by this flag may seem 251*0Sstevel@tonic-gatestraightforward, it should be used only if there is a good reason to do 252*0Sstevel@tonic-gateso. The reason for being cautious is that even if you have specified 253*0Sstevel@tonic-gatethe G_NOARGS flag, it is still possible for the Perl subroutine that 254*0Sstevel@tonic-gatehas been called to think that you have passed it parameters. 255*0Sstevel@tonic-gate 256*0Sstevel@tonic-gateIn fact, what can happen is that the Perl subroutine you have called 257*0Sstevel@tonic-gatecan access the C<@_> array from a previous Perl subroutine. This will 258*0Sstevel@tonic-gateoccur when the code that is executing the I<call_*> function has 259*0Sstevel@tonic-gateitself been called from another Perl subroutine. The code below 260*0Sstevel@tonic-gateillustrates this 261*0Sstevel@tonic-gate 262*0Sstevel@tonic-gate sub fred 263*0Sstevel@tonic-gate { print "@_\n" } 264*0Sstevel@tonic-gate 265*0Sstevel@tonic-gate sub joe 266*0Sstevel@tonic-gate { &fred } 267*0Sstevel@tonic-gate 268*0Sstevel@tonic-gate &joe(1,2,3) ; 269*0Sstevel@tonic-gate 270*0Sstevel@tonic-gateThis will print 271*0Sstevel@tonic-gate 272*0Sstevel@tonic-gate 1 2 3 273*0Sstevel@tonic-gate 274*0Sstevel@tonic-gateWhat has happened is that C<fred> accesses the C<@_> array which 275*0Sstevel@tonic-gatebelongs to C<joe>. 276*0Sstevel@tonic-gate 277*0Sstevel@tonic-gate 278*0Sstevel@tonic-gate=head2 G_EVAL 279*0Sstevel@tonic-gate 280*0Sstevel@tonic-gateIt is possible for the Perl subroutine you are calling to terminate 281*0Sstevel@tonic-gateabnormally, e.g., by calling I<die> explicitly or by not actually 282*0Sstevel@tonic-gateexisting. By default, when either of these events occurs, the 283*0Sstevel@tonic-gateprocess will terminate immediately. If you want to trap this 284*0Sstevel@tonic-gatetype of event, specify the G_EVAL flag. It will put an I<eval { }> 285*0Sstevel@tonic-gatearound the subroutine call. 286*0Sstevel@tonic-gate 287*0Sstevel@tonic-gateWhenever control returns from the I<call_*> function you need to 288*0Sstevel@tonic-gatecheck the C<$@> variable as you would in a normal Perl script. 289*0Sstevel@tonic-gate 290*0Sstevel@tonic-gateThe value returned from the I<call_*> function is dependent on 291*0Sstevel@tonic-gatewhat other flags have been specified and whether an error has 292*0Sstevel@tonic-gateoccurred. Here are all the different cases that can occur: 293*0Sstevel@tonic-gate 294*0Sstevel@tonic-gate=over 5 295*0Sstevel@tonic-gate 296*0Sstevel@tonic-gate=item * 297*0Sstevel@tonic-gate 298*0Sstevel@tonic-gateIf the I<call_*> function returns normally, then the value 299*0Sstevel@tonic-gatereturned is as specified in the previous sections. 300*0Sstevel@tonic-gate 301*0Sstevel@tonic-gate=item * 302*0Sstevel@tonic-gate 303*0Sstevel@tonic-gateIf G_DISCARD is specified, the return value will always be 0. 304*0Sstevel@tonic-gate 305*0Sstevel@tonic-gate=item * 306*0Sstevel@tonic-gate 307*0Sstevel@tonic-gateIf G_ARRAY is specified I<and> an error has occurred, the return value 308*0Sstevel@tonic-gatewill always be 0. 309*0Sstevel@tonic-gate 310*0Sstevel@tonic-gate=item * 311*0Sstevel@tonic-gate 312*0Sstevel@tonic-gateIf G_SCALAR is specified I<and> an error has occurred, the return value 313*0Sstevel@tonic-gatewill be 1 and the value on the top of the stack will be I<undef>. This 314*0Sstevel@tonic-gatemeans that if you have already detected the error by checking C<$@> and 315*0Sstevel@tonic-gateyou want the program to continue, you must remember to pop the I<undef> 316*0Sstevel@tonic-gatefrom the stack. 317*0Sstevel@tonic-gate 318*0Sstevel@tonic-gate=back 319*0Sstevel@tonic-gate 320*0Sstevel@tonic-gateSee I<Using G_EVAL> for details on using G_EVAL. 321*0Sstevel@tonic-gate 322*0Sstevel@tonic-gate=head2 G_KEEPERR 323*0Sstevel@tonic-gate 324*0Sstevel@tonic-gateYou may have noticed that using the G_EVAL flag described above will 325*0Sstevel@tonic-gateB<always> clear the C<$@> variable and set it to a string describing 326*0Sstevel@tonic-gatethe error iff there was an error in the called code. This unqualified 327*0Sstevel@tonic-gateresetting of C<$@> can be problematic in the reliable identification of 328*0Sstevel@tonic-gateerrors using the C<eval {}> mechanism, because the possibility exists 329*0Sstevel@tonic-gatethat perl will call other code (end of block processing code, for 330*0Sstevel@tonic-gateexample) between the time the error causes C<$@> to be set within 331*0Sstevel@tonic-gateC<eval {}>, and the subsequent statement which checks for the value of 332*0Sstevel@tonic-gateC<$@> gets executed in the user's script. 333*0Sstevel@tonic-gate 334*0Sstevel@tonic-gateThis scenario will mostly be applicable to code that is meant to be 335*0Sstevel@tonic-gatecalled from within destructors, asynchronous callbacks, signal 336*0Sstevel@tonic-gatehandlers, C<__DIE__> or C<__WARN__> hooks, and C<tie> functions. In 337*0Sstevel@tonic-gatesuch situations, you will not want to clear C<$@> at all, but simply to 338*0Sstevel@tonic-gateappend any new errors to any existing value of C<$@>. 339*0Sstevel@tonic-gate 340*0Sstevel@tonic-gateThe G_KEEPERR flag is meant to be used in conjunction with G_EVAL in 341*0Sstevel@tonic-gateI<call_*> functions that are used to implement such code. This flag 342*0Sstevel@tonic-gatehas no effect when G_EVAL is not used. 343*0Sstevel@tonic-gate 344*0Sstevel@tonic-gateWhen G_KEEPERR is used, any errors in the called code will be prefixed 345*0Sstevel@tonic-gatewith the string "\t(in cleanup)", and appended to the current value 346*0Sstevel@tonic-gateof C<$@>. 347*0Sstevel@tonic-gate 348*0Sstevel@tonic-gateThe G_KEEPERR flag was introduced in Perl version 5.002. 349*0Sstevel@tonic-gate 350*0Sstevel@tonic-gateSee I<Using G_KEEPERR> for an example of a situation that warrants the 351*0Sstevel@tonic-gateuse of this flag. 352*0Sstevel@tonic-gate 353*0Sstevel@tonic-gate=head2 Determining the Context 354*0Sstevel@tonic-gate 355*0Sstevel@tonic-gateAs mentioned above, you can determine the context of the currently 356*0Sstevel@tonic-gateexecuting subroutine in Perl with I<wantarray>. The equivalent test 357*0Sstevel@tonic-gatecan be made in C by using the C<GIMME_V> macro, which returns 358*0Sstevel@tonic-gateC<G_ARRAY> if you have been called in a list context, C<G_SCALAR> if 359*0Sstevel@tonic-gatein a scalar context, or C<G_VOID> if in a void context (i.e. the 360*0Sstevel@tonic-gatereturn value will not be used). An older version of this macro is 361*0Sstevel@tonic-gatecalled C<GIMME>; in a void context it returns C<G_SCALAR> instead of 362*0Sstevel@tonic-gateC<G_VOID>. An example of using the C<GIMME_V> macro is shown in 363*0Sstevel@tonic-gatesection I<Using GIMME_V>. 364*0Sstevel@tonic-gate 365*0Sstevel@tonic-gate=head1 KNOWN PROBLEMS 366*0Sstevel@tonic-gate 367*0Sstevel@tonic-gateThis section outlines all known problems that exist in the 368*0Sstevel@tonic-gateI<call_*> functions. 369*0Sstevel@tonic-gate 370*0Sstevel@tonic-gate=over 5 371*0Sstevel@tonic-gate 372*0Sstevel@tonic-gate=item 1. 373*0Sstevel@tonic-gate 374*0Sstevel@tonic-gateIf you are intending to make use of both the G_EVAL and G_SCALAR flags 375*0Sstevel@tonic-gatein your code, use a version of Perl greater than 5.000. There is a bug 376*0Sstevel@tonic-gatein version 5.000 of Perl which means that the combination of these two 377*0Sstevel@tonic-gateflags will not work as described in the section I<FLAG VALUES>. 378*0Sstevel@tonic-gate 379*0Sstevel@tonic-gateSpecifically, if the two flags are used when calling a subroutine and 380*0Sstevel@tonic-gatethat subroutine does not call I<die>, the value returned by 381*0Sstevel@tonic-gateI<call_*> will be wrong. 382*0Sstevel@tonic-gate 383*0Sstevel@tonic-gate 384*0Sstevel@tonic-gate=item 2. 385*0Sstevel@tonic-gate 386*0Sstevel@tonic-gateIn Perl 5.000 and 5.001 there is a problem with using I<call_*> if 387*0Sstevel@tonic-gatethe Perl sub you are calling attempts to trap a I<die>. 388*0Sstevel@tonic-gate 389*0Sstevel@tonic-gateThe symptom of this problem is that the called Perl sub will continue 390*0Sstevel@tonic-gateto completion, but whenever it attempts to pass control back to the 391*0Sstevel@tonic-gateXSUB, the program will immediately terminate. 392*0Sstevel@tonic-gate 393*0Sstevel@tonic-gateFor example, say you want to call this Perl sub 394*0Sstevel@tonic-gate 395*0Sstevel@tonic-gate sub fred 396*0Sstevel@tonic-gate { 397*0Sstevel@tonic-gate eval { die "Fatal Error" ; } 398*0Sstevel@tonic-gate print "Trapped error: $@\n" 399*0Sstevel@tonic-gate if $@ ; 400*0Sstevel@tonic-gate } 401*0Sstevel@tonic-gate 402*0Sstevel@tonic-gatevia this XSUB 403*0Sstevel@tonic-gate 404*0Sstevel@tonic-gate void 405*0Sstevel@tonic-gate Call_fred() 406*0Sstevel@tonic-gate CODE: 407*0Sstevel@tonic-gate PUSHMARK(SP) ; 408*0Sstevel@tonic-gate call_pv("fred", G_DISCARD|G_NOARGS) ; 409*0Sstevel@tonic-gate fprintf(stderr, "back in Call_fred\n") ; 410*0Sstevel@tonic-gate 411*0Sstevel@tonic-gateWhen C<Call_fred> is executed it will print 412*0Sstevel@tonic-gate 413*0Sstevel@tonic-gate Trapped error: Fatal Error 414*0Sstevel@tonic-gate 415*0Sstevel@tonic-gateAs control never returns to C<Call_fred>, the C<"back in Call_fred"> 416*0Sstevel@tonic-gatestring will not get printed. 417*0Sstevel@tonic-gate 418*0Sstevel@tonic-gateTo work around this problem, you can either upgrade to Perl 5.002 or 419*0Sstevel@tonic-gatehigher, or use the G_EVAL flag with I<call_*> as shown below 420*0Sstevel@tonic-gate 421*0Sstevel@tonic-gate void 422*0Sstevel@tonic-gate Call_fred() 423*0Sstevel@tonic-gate CODE: 424*0Sstevel@tonic-gate PUSHMARK(SP) ; 425*0Sstevel@tonic-gate call_pv("fred", G_EVAL|G_DISCARD|G_NOARGS) ; 426*0Sstevel@tonic-gate fprintf(stderr, "back in Call_fred\n") ; 427*0Sstevel@tonic-gate 428*0Sstevel@tonic-gate=back 429*0Sstevel@tonic-gate 430*0Sstevel@tonic-gate 431*0Sstevel@tonic-gate 432*0Sstevel@tonic-gate=head1 EXAMPLES 433*0Sstevel@tonic-gate 434*0Sstevel@tonic-gateEnough of the definition talk, let's have a few examples. 435*0Sstevel@tonic-gate 436*0Sstevel@tonic-gatePerl provides many macros to assist in accessing the Perl stack. 437*0Sstevel@tonic-gateWherever possible, these macros should always be used when interfacing 438*0Sstevel@tonic-gateto Perl internals. We hope this should make the code less vulnerable 439*0Sstevel@tonic-gateto any changes made to Perl in the future. 440*0Sstevel@tonic-gate 441*0Sstevel@tonic-gateAnother point worth noting is that in the first series of examples I 442*0Sstevel@tonic-gatehave made use of only the I<call_pv> function. This has been done 443*0Sstevel@tonic-gateto keep the code simpler and ease you into the topic. Wherever 444*0Sstevel@tonic-gatepossible, if the choice is between using I<call_pv> and 445*0Sstevel@tonic-gateI<call_sv>, you should always try to use I<call_sv>. See 446*0Sstevel@tonic-gateI<Using call_sv> for details. 447*0Sstevel@tonic-gate 448*0Sstevel@tonic-gate=head2 No Parameters, Nothing returned 449*0Sstevel@tonic-gate 450*0Sstevel@tonic-gateThis first trivial example will call a Perl subroutine, I<PrintUID>, to 451*0Sstevel@tonic-gateprint out the UID of the process. 452*0Sstevel@tonic-gate 453*0Sstevel@tonic-gate sub PrintUID 454*0Sstevel@tonic-gate { 455*0Sstevel@tonic-gate print "UID is $<\n" ; 456*0Sstevel@tonic-gate } 457*0Sstevel@tonic-gate 458*0Sstevel@tonic-gateand here is a C function to call it 459*0Sstevel@tonic-gate 460*0Sstevel@tonic-gate static void 461*0Sstevel@tonic-gate call_PrintUID() 462*0Sstevel@tonic-gate { 463*0Sstevel@tonic-gate dSP ; 464*0Sstevel@tonic-gate 465*0Sstevel@tonic-gate PUSHMARK(SP) ; 466*0Sstevel@tonic-gate call_pv("PrintUID", G_DISCARD|G_NOARGS) ; 467*0Sstevel@tonic-gate } 468*0Sstevel@tonic-gate 469*0Sstevel@tonic-gateSimple, eh. 470*0Sstevel@tonic-gate 471*0Sstevel@tonic-gateA few points to note about this example. 472*0Sstevel@tonic-gate 473*0Sstevel@tonic-gate=over 5 474*0Sstevel@tonic-gate 475*0Sstevel@tonic-gate=item 1. 476*0Sstevel@tonic-gate 477*0Sstevel@tonic-gateIgnore C<dSP> and C<PUSHMARK(SP)> for now. They will be discussed in 478*0Sstevel@tonic-gatethe next example. 479*0Sstevel@tonic-gate 480*0Sstevel@tonic-gate=item 2. 481*0Sstevel@tonic-gate 482*0Sstevel@tonic-gateWe aren't passing any parameters to I<PrintUID> so G_NOARGS can be 483*0Sstevel@tonic-gatespecified. 484*0Sstevel@tonic-gate 485*0Sstevel@tonic-gate=item 3. 486*0Sstevel@tonic-gate 487*0Sstevel@tonic-gateWe aren't interested in anything returned from I<PrintUID>, so 488*0Sstevel@tonic-gateG_DISCARD is specified. Even if I<PrintUID> was changed to 489*0Sstevel@tonic-gatereturn some value(s), having specified G_DISCARD will mean that they 490*0Sstevel@tonic-gatewill be wiped by the time control returns from I<call_pv>. 491*0Sstevel@tonic-gate 492*0Sstevel@tonic-gate=item 4. 493*0Sstevel@tonic-gate 494*0Sstevel@tonic-gateAs I<call_pv> is being used, the Perl subroutine is specified as a 495*0Sstevel@tonic-gateC string. In this case the subroutine name has been 'hard-wired' into the 496*0Sstevel@tonic-gatecode. 497*0Sstevel@tonic-gate 498*0Sstevel@tonic-gate=item 5. 499*0Sstevel@tonic-gate 500*0Sstevel@tonic-gateBecause we specified G_DISCARD, it is not necessary to check the value 501*0Sstevel@tonic-gatereturned from I<call_pv>. It will always be 0. 502*0Sstevel@tonic-gate 503*0Sstevel@tonic-gate=back 504*0Sstevel@tonic-gate 505*0Sstevel@tonic-gate=head2 Passing Parameters 506*0Sstevel@tonic-gate 507*0Sstevel@tonic-gateNow let's make a slightly more complex example. This time we want to 508*0Sstevel@tonic-gatecall a Perl subroutine, C<LeftString>, which will take 2 parameters--a 509*0Sstevel@tonic-gatestring ($s) and an integer ($n). The subroutine will simply 510*0Sstevel@tonic-gateprint the first $n characters of the string. 511*0Sstevel@tonic-gate 512*0Sstevel@tonic-gateSo the Perl subroutine would look like this 513*0Sstevel@tonic-gate 514*0Sstevel@tonic-gate sub LeftString 515*0Sstevel@tonic-gate { 516*0Sstevel@tonic-gate my($s, $n) = @_ ; 517*0Sstevel@tonic-gate print substr($s, 0, $n), "\n" ; 518*0Sstevel@tonic-gate } 519*0Sstevel@tonic-gate 520*0Sstevel@tonic-gateThe C function required to call I<LeftString> would look like this. 521*0Sstevel@tonic-gate 522*0Sstevel@tonic-gate static void 523*0Sstevel@tonic-gate call_LeftString(a, b) 524*0Sstevel@tonic-gate char * a ; 525*0Sstevel@tonic-gate int b ; 526*0Sstevel@tonic-gate { 527*0Sstevel@tonic-gate dSP ; 528*0Sstevel@tonic-gate 529*0Sstevel@tonic-gate ENTER ; 530*0Sstevel@tonic-gate SAVETMPS ; 531*0Sstevel@tonic-gate 532*0Sstevel@tonic-gate PUSHMARK(SP) ; 533*0Sstevel@tonic-gate XPUSHs(sv_2mortal(newSVpv(a, 0))); 534*0Sstevel@tonic-gate XPUSHs(sv_2mortal(newSViv(b))); 535*0Sstevel@tonic-gate PUTBACK ; 536*0Sstevel@tonic-gate 537*0Sstevel@tonic-gate call_pv("LeftString", G_DISCARD); 538*0Sstevel@tonic-gate 539*0Sstevel@tonic-gate FREETMPS ; 540*0Sstevel@tonic-gate LEAVE ; 541*0Sstevel@tonic-gate } 542*0Sstevel@tonic-gate 543*0Sstevel@tonic-gateHere are a few notes on the C function I<call_LeftString>. 544*0Sstevel@tonic-gate 545*0Sstevel@tonic-gate=over 5 546*0Sstevel@tonic-gate 547*0Sstevel@tonic-gate=item 1. 548*0Sstevel@tonic-gate 549*0Sstevel@tonic-gateParameters are passed to the Perl subroutine using the Perl stack. 550*0Sstevel@tonic-gateThis is the purpose of the code beginning with the line C<dSP> and 551*0Sstevel@tonic-gateending with the line C<PUTBACK>. The C<dSP> declares a local copy 552*0Sstevel@tonic-gateof the stack pointer. This local copy should B<always> be accessed 553*0Sstevel@tonic-gateas C<SP>. 554*0Sstevel@tonic-gate 555*0Sstevel@tonic-gate=item 2. 556*0Sstevel@tonic-gate 557*0Sstevel@tonic-gateIf you are going to put something onto the Perl stack, you need to know 558*0Sstevel@tonic-gatewhere to put it. This is the purpose of the macro C<dSP>--it declares 559*0Sstevel@tonic-gateand initializes a I<local> copy of the Perl stack pointer. 560*0Sstevel@tonic-gate 561*0Sstevel@tonic-gateAll the other macros which will be used in this example require you to 562*0Sstevel@tonic-gatehave used this macro. 563*0Sstevel@tonic-gate 564*0Sstevel@tonic-gateThe exception to this rule is if you are calling a Perl subroutine 565*0Sstevel@tonic-gatedirectly from an XSUB function. In this case it is not necessary to 566*0Sstevel@tonic-gateuse the C<dSP> macro explicitly--it will be declared for you 567*0Sstevel@tonic-gateautomatically. 568*0Sstevel@tonic-gate 569*0Sstevel@tonic-gate=item 3. 570*0Sstevel@tonic-gate 571*0Sstevel@tonic-gateAny parameters to be pushed onto the stack should be bracketed by the 572*0Sstevel@tonic-gateC<PUSHMARK> and C<PUTBACK> macros. The purpose of these two macros, in 573*0Sstevel@tonic-gatethis context, is to count the number of parameters you are 574*0Sstevel@tonic-gatepushing automatically. Then whenever Perl is creating the C<@_> array for the 575*0Sstevel@tonic-gatesubroutine, it knows how big to make it. 576*0Sstevel@tonic-gate 577*0Sstevel@tonic-gateThe C<PUSHMARK> macro tells Perl to make a mental note of the current 578*0Sstevel@tonic-gatestack pointer. Even if you aren't passing any parameters (like the 579*0Sstevel@tonic-gateexample shown in the section I<No Parameters, Nothing returned>) you 580*0Sstevel@tonic-gatemust still call the C<PUSHMARK> macro before you can call any of the 581*0Sstevel@tonic-gateI<call_*> functions--Perl still needs to know that there are no 582*0Sstevel@tonic-gateparameters. 583*0Sstevel@tonic-gate 584*0Sstevel@tonic-gateThe C<PUTBACK> macro sets the global copy of the stack pointer to be 585*0Sstevel@tonic-gatethe same as our local copy. If we didn't do this I<call_pv> 586*0Sstevel@tonic-gatewouldn't know where the two parameters we pushed were--remember that 587*0Sstevel@tonic-gateup to now all the stack pointer manipulation we have done is with our 588*0Sstevel@tonic-gatelocal copy, I<not> the global copy. 589*0Sstevel@tonic-gate 590*0Sstevel@tonic-gate=item 4. 591*0Sstevel@tonic-gate 592*0Sstevel@tonic-gateNext, we come to XPUSHs. This is where the parameters actually get 593*0Sstevel@tonic-gatepushed onto the stack. In this case we are pushing a string and an 594*0Sstevel@tonic-gateinteger. 595*0Sstevel@tonic-gate 596*0Sstevel@tonic-gateSee L<perlguts/"XSUBs and the Argument Stack"> for details 597*0Sstevel@tonic-gateon how the XPUSH macros work. 598*0Sstevel@tonic-gate 599*0Sstevel@tonic-gate=item 5. 600*0Sstevel@tonic-gate 601*0Sstevel@tonic-gateBecause we created temporary values (by means of sv_2mortal() calls) 602*0Sstevel@tonic-gatewe will have to tidy up the Perl stack and dispose of mortal SVs. 603*0Sstevel@tonic-gate 604*0Sstevel@tonic-gateThis is the purpose of 605*0Sstevel@tonic-gate 606*0Sstevel@tonic-gate ENTER ; 607*0Sstevel@tonic-gate SAVETMPS ; 608*0Sstevel@tonic-gate 609*0Sstevel@tonic-gateat the start of the function, and 610*0Sstevel@tonic-gate 611*0Sstevel@tonic-gate FREETMPS ; 612*0Sstevel@tonic-gate LEAVE ; 613*0Sstevel@tonic-gate 614*0Sstevel@tonic-gateat the end. The C<ENTER>/C<SAVETMPS> pair creates a boundary for any 615*0Sstevel@tonic-gatetemporaries we create. This means that the temporaries we get rid of 616*0Sstevel@tonic-gatewill be limited to those which were created after these calls. 617*0Sstevel@tonic-gate 618*0Sstevel@tonic-gateThe C<FREETMPS>/C<LEAVE> pair will get rid of any values returned by 619*0Sstevel@tonic-gatethe Perl subroutine (see next example), plus it will also dump the 620*0Sstevel@tonic-gatemortal SVs we have created. Having C<ENTER>/C<SAVETMPS> at the 621*0Sstevel@tonic-gatebeginning of the code makes sure that no other mortals are destroyed. 622*0Sstevel@tonic-gate 623*0Sstevel@tonic-gateThink of these macros as working a bit like using C<{> and C<}> in Perl 624*0Sstevel@tonic-gateto limit the scope of local variables. 625*0Sstevel@tonic-gate 626*0Sstevel@tonic-gateSee the section I<Using Perl to dispose of temporaries> for details of 627*0Sstevel@tonic-gatean alternative to using these macros. 628*0Sstevel@tonic-gate 629*0Sstevel@tonic-gate=item 6. 630*0Sstevel@tonic-gate 631*0Sstevel@tonic-gateFinally, I<LeftString> can now be called via the I<call_pv> function. 632*0Sstevel@tonic-gateThe only flag specified this time is G_DISCARD. Because we are passing 633*0Sstevel@tonic-gate2 parameters to the Perl subroutine this time, we have not specified 634*0Sstevel@tonic-gateG_NOARGS. 635*0Sstevel@tonic-gate 636*0Sstevel@tonic-gate=back 637*0Sstevel@tonic-gate 638*0Sstevel@tonic-gate=head2 Returning a Scalar 639*0Sstevel@tonic-gate 640*0Sstevel@tonic-gateNow for an example of dealing with the items returned from a Perl 641*0Sstevel@tonic-gatesubroutine. 642*0Sstevel@tonic-gate 643*0Sstevel@tonic-gateHere is a Perl subroutine, I<Adder>, that takes 2 integer parameters 644*0Sstevel@tonic-gateand simply returns their sum. 645*0Sstevel@tonic-gate 646*0Sstevel@tonic-gate sub Adder 647*0Sstevel@tonic-gate { 648*0Sstevel@tonic-gate my($a, $b) = @_ ; 649*0Sstevel@tonic-gate $a + $b ; 650*0Sstevel@tonic-gate } 651*0Sstevel@tonic-gate 652*0Sstevel@tonic-gateBecause we are now concerned with the return value from I<Adder>, the C 653*0Sstevel@tonic-gatefunction required to call it is now a bit more complex. 654*0Sstevel@tonic-gate 655*0Sstevel@tonic-gate static void 656*0Sstevel@tonic-gate call_Adder(a, b) 657*0Sstevel@tonic-gate int a ; 658*0Sstevel@tonic-gate int b ; 659*0Sstevel@tonic-gate { 660*0Sstevel@tonic-gate dSP ; 661*0Sstevel@tonic-gate int count ; 662*0Sstevel@tonic-gate 663*0Sstevel@tonic-gate ENTER ; 664*0Sstevel@tonic-gate SAVETMPS; 665*0Sstevel@tonic-gate 666*0Sstevel@tonic-gate PUSHMARK(SP) ; 667*0Sstevel@tonic-gate XPUSHs(sv_2mortal(newSViv(a))); 668*0Sstevel@tonic-gate XPUSHs(sv_2mortal(newSViv(b))); 669*0Sstevel@tonic-gate PUTBACK ; 670*0Sstevel@tonic-gate 671*0Sstevel@tonic-gate count = call_pv("Adder", G_SCALAR); 672*0Sstevel@tonic-gate 673*0Sstevel@tonic-gate SPAGAIN ; 674*0Sstevel@tonic-gate 675*0Sstevel@tonic-gate if (count != 1) 676*0Sstevel@tonic-gate croak("Big trouble\n") ; 677*0Sstevel@tonic-gate 678*0Sstevel@tonic-gate printf ("The sum of %d and %d is %d\n", a, b, POPi) ; 679*0Sstevel@tonic-gate 680*0Sstevel@tonic-gate PUTBACK ; 681*0Sstevel@tonic-gate FREETMPS ; 682*0Sstevel@tonic-gate LEAVE ; 683*0Sstevel@tonic-gate } 684*0Sstevel@tonic-gate 685*0Sstevel@tonic-gatePoints to note this time are 686*0Sstevel@tonic-gate 687*0Sstevel@tonic-gate=over 5 688*0Sstevel@tonic-gate 689*0Sstevel@tonic-gate=item 1. 690*0Sstevel@tonic-gate 691*0Sstevel@tonic-gateThe only flag specified this time was G_SCALAR. That means the C<@_> 692*0Sstevel@tonic-gatearray will be created and that the value returned by I<Adder> will 693*0Sstevel@tonic-gatestill exist after the call to I<call_pv>. 694*0Sstevel@tonic-gate 695*0Sstevel@tonic-gate=item 2. 696*0Sstevel@tonic-gate 697*0Sstevel@tonic-gateThe purpose of the macro C<SPAGAIN> is to refresh the local copy of the 698*0Sstevel@tonic-gatestack pointer. This is necessary because it is possible that the memory 699*0Sstevel@tonic-gateallocated to the Perl stack has been reallocated whilst in the 700*0Sstevel@tonic-gateI<call_pv> call. 701*0Sstevel@tonic-gate 702*0Sstevel@tonic-gateIf you are making use of the Perl stack pointer in your code you must 703*0Sstevel@tonic-gatealways refresh the local copy using SPAGAIN whenever you make use 704*0Sstevel@tonic-gateof the I<call_*> functions or any other Perl internal function. 705*0Sstevel@tonic-gate 706*0Sstevel@tonic-gate=item 3. 707*0Sstevel@tonic-gate 708*0Sstevel@tonic-gateAlthough only a single value was expected to be returned from I<Adder>, 709*0Sstevel@tonic-gateit is still good practice to check the return code from I<call_pv> 710*0Sstevel@tonic-gateanyway. 711*0Sstevel@tonic-gate 712*0Sstevel@tonic-gateExpecting a single value is not quite the same as knowing that there 713*0Sstevel@tonic-gatewill be one. If someone modified I<Adder> to return a list and we 714*0Sstevel@tonic-gatedidn't check for that possibility and take appropriate action the Perl 715*0Sstevel@tonic-gatestack would end up in an inconsistent state. That is something you 716*0Sstevel@tonic-gateI<really> don't want to happen ever. 717*0Sstevel@tonic-gate 718*0Sstevel@tonic-gate=item 4. 719*0Sstevel@tonic-gate 720*0Sstevel@tonic-gateThe C<POPi> macro is used here to pop the return value from the stack. 721*0Sstevel@tonic-gateIn this case we wanted an integer, so C<POPi> was used. 722*0Sstevel@tonic-gate 723*0Sstevel@tonic-gate 724*0Sstevel@tonic-gateHere is the complete list of POP macros available, along with the types 725*0Sstevel@tonic-gatethey return. 726*0Sstevel@tonic-gate 727*0Sstevel@tonic-gate POPs SV 728*0Sstevel@tonic-gate POPp pointer 729*0Sstevel@tonic-gate POPn double 730*0Sstevel@tonic-gate POPi integer 731*0Sstevel@tonic-gate POPl long 732*0Sstevel@tonic-gate 733*0Sstevel@tonic-gate=item 5. 734*0Sstevel@tonic-gate 735*0Sstevel@tonic-gateThe final C<PUTBACK> is used to leave the Perl stack in a consistent 736*0Sstevel@tonic-gatestate before exiting the function. This is necessary because when we 737*0Sstevel@tonic-gatepopped the return value from the stack with C<POPi> it updated only our 738*0Sstevel@tonic-gatelocal copy of the stack pointer. Remember, C<PUTBACK> sets the global 739*0Sstevel@tonic-gatestack pointer to be the same as our local copy. 740*0Sstevel@tonic-gate 741*0Sstevel@tonic-gate=back 742*0Sstevel@tonic-gate 743*0Sstevel@tonic-gate 744*0Sstevel@tonic-gate=head2 Returning a list of values 745*0Sstevel@tonic-gate 746*0Sstevel@tonic-gateNow, let's extend the previous example to return both the sum of the 747*0Sstevel@tonic-gateparameters and the difference. 748*0Sstevel@tonic-gate 749*0Sstevel@tonic-gateHere is the Perl subroutine 750*0Sstevel@tonic-gate 751*0Sstevel@tonic-gate sub AddSubtract 752*0Sstevel@tonic-gate { 753*0Sstevel@tonic-gate my($a, $b) = @_ ; 754*0Sstevel@tonic-gate ($a+$b, $a-$b) ; 755*0Sstevel@tonic-gate } 756*0Sstevel@tonic-gate 757*0Sstevel@tonic-gateand this is the C function 758*0Sstevel@tonic-gate 759*0Sstevel@tonic-gate static void 760*0Sstevel@tonic-gate call_AddSubtract(a, b) 761*0Sstevel@tonic-gate int a ; 762*0Sstevel@tonic-gate int b ; 763*0Sstevel@tonic-gate { 764*0Sstevel@tonic-gate dSP ; 765*0Sstevel@tonic-gate int count ; 766*0Sstevel@tonic-gate 767*0Sstevel@tonic-gate ENTER ; 768*0Sstevel@tonic-gate SAVETMPS; 769*0Sstevel@tonic-gate 770*0Sstevel@tonic-gate PUSHMARK(SP) ; 771*0Sstevel@tonic-gate XPUSHs(sv_2mortal(newSViv(a))); 772*0Sstevel@tonic-gate XPUSHs(sv_2mortal(newSViv(b))); 773*0Sstevel@tonic-gate PUTBACK ; 774*0Sstevel@tonic-gate 775*0Sstevel@tonic-gate count = call_pv("AddSubtract", G_ARRAY); 776*0Sstevel@tonic-gate 777*0Sstevel@tonic-gate SPAGAIN ; 778*0Sstevel@tonic-gate 779*0Sstevel@tonic-gate if (count != 2) 780*0Sstevel@tonic-gate croak("Big trouble\n") ; 781*0Sstevel@tonic-gate 782*0Sstevel@tonic-gate printf ("%d - %d = %d\n", a, b, POPi) ; 783*0Sstevel@tonic-gate printf ("%d + %d = %d\n", a, b, POPi) ; 784*0Sstevel@tonic-gate 785*0Sstevel@tonic-gate PUTBACK ; 786*0Sstevel@tonic-gate FREETMPS ; 787*0Sstevel@tonic-gate LEAVE ; 788*0Sstevel@tonic-gate } 789*0Sstevel@tonic-gate 790*0Sstevel@tonic-gateIf I<call_AddSubtract> is called like this 791*0Sstevel@tonic-gate 792*0Sstevel@tonic-gate call_AddSubtract(7, 4) ; 793*0Sstevel@tonic-gate 794*0Sstevel@tonic-gatethen here is the output 795*0Sstevel@tonic-gate 796*0Sstevel@tonic-gate 7 - 4 = 3 797*0Sstevel@tonic-gate 7 + 4 = 11 798*0Sstevel@tonic-gate 799*0Sstevel@tonic-gateNotes 800*0Sstevel@tonic-gate 801*0Sstevel@tonic-gate=over 5 802*0Sstevel@tonic-gate 803*0Sstevel@tonic-gate=item 1. 804*0Sstevel@tonic-gate 805*0Sstevel@tonic-gateWe wanted list context, so G_ARRAY was used. 806*0Sstevel@tonic-gate 807*0Sstevel@tonic-gate=item 2. 808*0Sstevel@tonic-gate 809*0Sstevel@tonic-gateNot surprisingly C<POPi> is used twice this time because we were 810*0Sstevel@tonic-gateretrieving 2 values from the stack. The important thing to note is that 811*0Sstevel@tonic-gatewhen using the C<POP*> macros they come off the stack in I<reverse> 812*0Sstevel@tonic-gateorder. 813*0Sstevel@tonic-gate 814*0Sstevel@tonic-gate=back 815*0Sstevel@tonic-gate 816*0Sstevel@tonic-gate=head2 Returning a list in a scalar context 817*0Sstevel@tonic-gate 818*0Sstevel@tonic-gateSay the Perl subroutine in the previous section was called in a scalar 819*0Sstevel@tonic-gatecontext, like this 820*0Sstevel@tonic-gate 821*0Sstevel@tonic-gate static void 822*0Sstevel@tonic-gate call_AddSubScalar(a, b) 823*0Sstevel@tonic-gate int a ; 824*0Sstevel@tonic-gate int b ; 825*0Sstevel@tonic-gate { 826*0Sstevel@tonic-gate dSP ; 827*0Sstevel@tonic-gate int count ; 828*0Sstevel@tonic-gate int i ; 829*0Sstevel@tonic-gate 830*0Sstevel@tonic-gate ENTER ; 831*0Sstevel@tonic-gate SAVETMPS; 832*0Sstevel@tonic-gate 833*0Sstevel@tonic-gate PUSHMARK(SP) ; 834*0Sstevel@tonic-gate XPUSHs(sv_2mortal(newSViv(a))); 835*0Sstevel@tonic-gate XPUSHs(sv_2mortal(newSViv(b))); 836*0Sstevel@tonic-gate PUTBACK ; 837*0Sstevel@tonic-gate 838*0Sstevel@tonic-gate count = call_pv("AddSubtract", G_SCALAR); 839*0Sstevel@tonic-gate 840*0Sstevel@tonic-gate SPAGAIN ; 841*0Sstevel@tonic-gate 842*0Sstevel@tonic-gate printf ("Items Returned = %d\n", count) ; 843*0Sstevel@tonic-gate 844*0Sstevel@tonic-gate for (i = 1 ; i <= count ; ++i) 845*0Sstevel@tonic-gate printf ("Value %d = %d\n", i, POPi) ; 846*0Sstevel@tonic-gate 847*0Sstevel@tonic-gate PUTBACK ; 848*0Sstevel@tonic-gate FREETMPS ; 849*0Sstevel@tonic-gate LEAVE ; 850*0Sstevel@tonic-gate } 851*0Sstevel@tonic-gate 852*0Sstevel@tonic-gateThe other modification made is that I<call_AddSubScalar> will print the 853*0Sstevel@tonic-gatenumber of items returned from the Perl subroutine and their value (for 854*0Sstevel@tonic-gatesimplicity it assumes that they are integer). So if 855*0Sstevel@tonic-gateI<call_AddSubScalar> is called 856*0Sstevel@tonic-gate 857*0Sstevel@tonic-gate call_AddSubScalar(7, 4) ; 858*0Sstevel@tonic-gate 859*0Sstevel@tonic-gatethen the output will be 860*0Sstevel@tonic-gate 861*0Sstevel@tonic-gate Items Returned = 1 862*0Sstevel@tonic-gate Value 1 = 3 863*0Sstevel@tonic-gate 864*0Sstevel@tonic-gateIn this case the main point to note is that only the last item in the 865*0Sstevel@tonic-gatelist is returned from the subroutine, I<AddSubtract> actually made it back to 866*0Sstevel@tonic-gateI<call_AddSubScalar>. 867*0Sstevel@tonic-gate 868*0Sstevel@tonic-gate 869*0Sstevel@tonic-gate=head2 Returning Data from Perl via the parameter list 870*0Sstevel@tonic-gate 871*0Sstevel@tonic-gateIt is also possible to return values directly via the parameter list - 872*0Sstevel@tonic-gatewhether it is actually desirable to do it is another matter entirely. 873*0Sstevel@tonic-gate 874*0Sstevel@tonic-gateThe Perl subroutine, I<Inc>, below takes 2 parameters and increments 875*0Sstevel@tonic-gateeach directly. 876*0Sstevel@tonic-gate 877*0Sstevel@tonic-gate sub Inc 878*0Sstevel@tonic-gate { 879*0Sstevel@tonic-gate ++ $_[0] ; 880*0Sstevel@tonic-gate ++ $_[1] ; 881*0Sstevel@tonic-gate } 882*0Sstevel@tonic-gate 883*0Sstevel@tonic-gateand here is a C function to call it. 884*0Sstevel@tonic-gate 885*0Sstevel@tonic-gate static void 886*0Sstevel@tonic-gate call_Inc(a, b) 887*0Sstevel@tonic-gate int a ; 888*0Sstevel@tonic-gate int b ; 889*0Sstevel@tonic-gate { 890*0Sstevel@tonic-gate dSP ; 891*0Sstevel@tonic-gate int count ; 892*0Sstevel@tonic-gate SV * sva ; 893*0Sstevel@tonic-gate SV * svb ; 894*0Sstevel@tonic-gate 895*0Sstevel@tonic-gate ENTER ; 896*0Sstevel@tonic-gate SAVETMPS; 897*0Sstevel@tonic-gate 898*0Sstevel@tonic-gate sva = sv_2mortal(newSViv(a)) ; 899*0Sstevel@tonic-gate svb = sv_2mortal(newSViv(b)) ; 900*0Sstevel@tonic-gate 901*0Sstevel@tonic-gate PUSHMARK(SP) ; 902*0Sstevel@tonic-gate XPUSHs(sva); 903*0Sstevel@tonic-gate XPUSHs(svb); 904*0Sstevel@tonic-gate PUTBACK ; 905*0Sstevel@tonic-gate 906*0Sstevel@tonic-gate count = call_pv("Inc", G_DISCARD); 907*0Sstevel@tonic-gate 908*0Sstevel@tonic-gate if (count != 0) 909*0Sstevel@tonic-gate croak ("call_Inc: expected 0 values from 'Inc', got %d\n", 910*0Sstevel@tonic-gate count) ; 911*0Sstevel@tonic-gate 912*0Sstevel@tonic-gate printf ("%d + 1 = %d\n", a, SvIV(sva)) ; 913*0Sstevel@tonic-gate printf ("%d + 1 = %d\n", b, SvIV(svb)) ; 914*0Sstevel@tonic-gate 915*0Sstevel@tonic-gate FREETMPS ; 916*0Sstevel@tonic-gate LEAVE ; 917*0Sstevel@tonic-gate } 918*0Sstevel@tonic-gate 919*0Sstevel@tonic-gateTo be able to access the two parameters that were pushed onto the stack 920*0Sstevel@tonic-gateafter they return from I<call_pv> it is necessary to make a note 921*0Sstevel@tonic-gateof their addresses--thus the two variables C<sva> and C<svb>. 922*0Sstevel@tonic-gate 923*0Sstevel@tonic-gateThe reason this is necessary is that the area of the Perl stack which 924*0Sstevel@tonic-gateheld them will very likely have been overwritten by something else by 925*0Sstevel@tonic-gatethe time control returns from I<call_pv>. 926*0Sstevel@tonic-gate 927*0Sstevel@tonic-gate 928*0Sstevel@tonic-gate 929*0Sstevel@tonic-gate 930*0Sstevel@tonic-gate=head2 Using G_EVAL 931*0Sstevel@tonic-gate 932*0Sstevel@tonic-gateNow an example using G_EVAL. Below is a Perl subroutine which computes 933*0Sstevel@tonic-gatethe difference of its 2 parameters. If this would result in a negative 934*0Sstevel@tonic-gateresult, the subroutine calls I<die>. 935*0Sstevel@tonic-gate 936*0Sstevel@tonic-gate sub Subtract 937*0Sstevel@tonic-gate { 938*0Sstevel@tonic-gate my ($a, $b) = @_ ; 939*0Sstevel@tonic-gate 940*0Sstevel@tonic-gate die "death can be fatal\n" if $a < $b ; 941*0Sstevel@tonic-gate 942*0Sstevel@tonic-gate $a - $b ; 943*0Sstevel@tonic-gate } 944*0Sstevel@tonic-gate 945*0Sstevel@tonic-gateand some C to call it 946*0Sstevel@tonic-gate 947*0Sstevel@tonic-gate static void 948*0Sstevel@tonic-gate call_Subtract(a, b) 949*0Sstevel@tonic-gate int a ; 950*0Sstevel@tonic-gate int b ; 951*0Sstevel@tonic-gate { 952*0Sstevel@tonic-gate dSP ; 953*0Sstevel@tonic-gate int count ; 954*0Sstevel@tonic-gate 955*0Sstevel@tonic-gate ENTER ; 956*0Sstevel@tonic-gate SAVETMPS; 957*0Sstevel@tonic-gate 958*0Sstevel@tonic-gate PUSHMARK(SP) ; 959*0Sstevel@tonic-gate XPUSHs(sv_2mortal(newSViv(a))); 960*0Sstevel@tonic-gate XPUSHs(sv_2mortal(newSViv(b))); 961*0Sstevel@tonic-gate PUTBACK ; 962*0Sstevel@tonic-gate 963*0Sstevel@tonic-gate count = call_pv("Subtract", G_EVAL|G_SCALAR); 964*0Sstevel@tonic-gate 965*0Sstevel@tonic-gate SPAGAIN ; 966*0Sstevel@tonic-gate 967*0Sstevel@tonic-gate /* Check the eval first */ 968*0Sstevel@tonic-gate if (SvTRUE(ERRSV)) 969*0Sstevel@tonic-gate { 970*0Sstevel@tonic-gate STRLEN n_a; 971*0Sstevel@tonic-gate printf ("Uh oh - %s\n", SvPV(ERRSV, n_a)) ; 972*0Sstevel@tonic-gate POPs ; 973*0Sstevel@tonic-gate } 974*0Sstevel@tonic-gate else 975*0Sstevel@tonic-gate { 976*0Sstevel@tonic-gate if (count != 1) 977*0Sstevel@tonic-gate croak("call_Subtract: wanted 1 value from 'Subtract', got %d\n", 978*0Sstevel@tonic-gate count) ; 979*0Sstevel@tonic-gate 980*0Sstevel@tonic-gate printf ("%d - %d = %d\n", a, b, POPi) ; 981*0Sstevel@tonic-gate } 982*0Sstevel@tonic-gate 983*0Sstevel@tonic-gate PUTBACK ; 984*0Sstevel@tonic-gate FREETMPS ; 985*0Sstevel@tonic-gate LEAVE ; 986*0Sstevel@tonic-gate } 987*0Sstevel@tonic-gate 988*0Sstevel@tonic-gateIf I<call_Subtract> is called thus 989*0Sstevel@tonic-gate 990*0Sstevel@tonic-gate call_Subtract(4, 5) 991*0Sstevel@tonic-gate 992*0Sstevel@tonic-gatethe following will be printed 993*0Sstevel@tonic-gate 994*0Sstevel@tonic-gate Uh oh - death can be fatal 995*0Sstevel@tonic-gate 996*0Sstevel@tonic-gateNotes 997*0Sstevel@tonic-gate 998*0Sstevel@tonic-gate=over 5 999*0Sstevel@tonic-gate 1000*0Sstevel@tonic-gate=item 1. 1001*0Sstevel@tonic-gate 1002*0Sstevel@tonic-gateWe want to be able to catch the I<die> so we have used the G_EVAL 1003*0Sstevel@tonic-gateflag. Not specifying this flag would mean that the program would 1004*0Sstevel@tonic-gateterminate immediately at the I<die> statement in the subroutine 1005*0Sstevel@tonic-gateI<Subtract>. 1006*0Sstevel@tonic-gate 1007*0Sstevel@tonic-gate=item 2. 1008*0Sstevel@tonic-gate 1009*0Sstevel@tonic-gateThe code 1010*0Sstevel@tonic-gate 1011*0Sstevel@tonic-gate if (SvTRUE(ERRSV)) 1012*0Sstevel@tonic-gate { 1013*0Sstevel@tonic-gate STRLEN n_a; 1014*0Sstevel@tonic-gate printf ("Uh oh - %s\n", SvPV(ERRSV, n_a)) ; 1015*0Sstevel@tonic-gate POPs ; 1016*0Sstevel@tonic-gate } 1017*0Sstevel@tonic-gate 1018*0Sstevel@tonic-gateis the direct equivalent of this bit of Perl 1019*0Sstevel@tonic-gate 1020*0Sstevel@tonic-gate print "Uh oh - $@\n" if $@ ; 1021*0Sstevel@tonic-gate 1022*0Sstevel@tonic-gateC<PL_errgv> is a perl global of type C<GV *> that points to the 1023*0Sstevel@tonic-gatesymbol table entry containing the error. C<ERRSV> therefore 1024*0Sstevel@tonic-gaterefers to the C equivalent of C<$@>. 1025*0Sstevel@tonic-gate 1026*0Sstevel@tonic-gate=item 3. 1027*0Sstevel@tonic-gate 1028*0Sstevel@tonic-gateNote that the stack is popped using C<POPs> in the block where 1029*0Sstevel@tonic-gateC<SvTRUE(ERRSV)> is true. This is necessary because whenever a 1030*0Sstevel@tonic-gateI<call_*> function invoked with G_EVAL|G_SCALAR returns an error, 1031*0Sstevel@tonic-gatethe top of the stack holds the value I<undef>. Because we want the 1032*0Sstevel@tonic-gateprogram to continue after detecting this error, it is essential that 1033*0Sstevel@tonic-gatethe stack is tidied up by removing the I<undef>. 1034*0Sstevel@tonic-gate 1035*0Sstevel@tonic-gate=back 1036*0Sstevel@tonic-gate 1037*0Sstevel@tonic-gate 1038*0Sstevel@tonic-gate=head2 Using G_KEEPERR 1039*0Sstevel@tonic-gate 1040*0Sstevel@tonic-gateConsider this rather facetious example, where we have used an XS 1041*0Sstevel@tonic-gateversion of the call_Subtract example above inside a destructor: 1042*0Sstevel@tonic-gate 1043*0Sstevel@tonic-gate package Foo; 1044*0Sstevel@tonic-gate sub new { bless {}, $_[0] } 1045*0Sstevel@tonic-gate sub Subtract { 1046*0Sstevel@tonic-gate my($a,$b) = @_; 1047*0Sstevel@tonic-gate die "death can be fatal" if $a < $b ; 1048*0Sstevel@tonic-gate $a - $b; 1049*0Sstevel@tonic-gate } 1050*0Sstevel@tonic-gate sub DESTROY { call_Subtract(5, 4); } 1051*0Sstevel@tonic-gate sub foo { die "foo dies"; } 1052*0Sstevel@tonic-gate 1053*0Sstevel@tonic-gate package main; 1054*0Sstevel@tonic-gate eval { Foo->new->foo }; 1055*0Sstevel@tonic-gate print "Saw: $@" if $@; # should be, but isn't 1056*0Sstevel@tonic-gate 1057*0Sstevel@tonic-gateThis example will fail to recognize that an error occurred inside the 1058*0Sstevel@tonic-gateC<eval {}>. Here's why: the call_Subtract code got executed while perl 1059*0Sstevel@tonic-gatewas cleaning up temporaries when exiting the eval block, and because 1060*0Sstevel@tonic-gatecall_Subtract is implemented with I<call_pv> using the G_EVAL 1061*0Sstevel@tonic-gateflag, it promptly reset C<$@>. This results in the failure of the 1062*0Sstevel@tonic-gateoutermost test for C<$@>, and thereby the failure of the error trap. 1063*0Sstevel@tonic-gate 1064*0Sstevel@tonic-gateAppending the G_KEEPERR flag, so that the I<call_pv> call in 1065*0Sstevel@tonic-gatecall_Subtract reads: 1066*0Sstevel@tonic-gate 1067*0Sstevel@tonic-gate count = call_pv("Subtract", G_EVAL|G_SCALAR|G_KEEPERR); 1068*0Sstevel@tonic-gate 1069*0Sstevel@tonic-gatewill preserve the error and restore reliable error handling. 1070*0Sstevel@tonic-gate 1071*0Sstevel@tonic-gate=head2 Using call_sv 1072*0Sstevel@tonic-gate 1073*0Sstevel@tonic-gateIn all the previous examples I have 'hard-wired' the name of the Perl 1074*0Sstevel@tonic-gatesubroutine to be called from C. Most of the time though, it is more 1075*0Sstevel@tonic-gateconvenient to be able to specify the name of the Perl subroutine from 1076*0Sstevel@tonic-gatewithin the Perl script. 1077*0Sstevel@tonic-gate 1078*0Sstevel@tonic-gateConsider the Perl code below 1079*0Sstevel@tonic-gate 1080*0Sstevel@tonic-gate sub fred 1081*0Sstevel@tonic-gate { 1082*0Sstevel@tonic-gate print "Hello there\n" ; 1083*0Sstevel@tonic-gate } 1084*0Sstevel@tonic-gate 1085*0Sstevel@tonic-gate CallSubPV("fred") ; 1086*0Sstevel@tonic-gate 1087*0Sstevel@tonic-gateHere is a snippet of XSUB which defines I<CallSubPV>. 1088*0Sstevel@tonic-gate 1089*0Sstevel@tonic-gate void 1090*0Sstevel@tonic-gate CallSubPV(name) 1091*0Sstevel@tonic-gate char * name 1092*0Sstevel@tonic-gate CODE: 1093*0Sstevel@tonic-gate PUSHMARK(SP) ; 1094*0Sstevel@tonic-gate call_pv(name, G_DISCARD|G_NOARGS) ; 1095*0Sstevel@tonic-gate 1096*0Sstevel@tonic-gateThat is fine as far as it goes. The thing is, the Perl subroutine 1097*0Sstevel@tonic-gatecan be specified as only a string. For Perl 4 this was adequate, 1098*0Sstevel@tonic-gatebut Perl 5 allows references to subroutines and anonymous subroutines. 1099*0Sstevel@tonic-gateThis is where I<call_sv> is useful. 1100*0Sstevel@tonic-gate 1101*0Sstevel@tonic-gateThe code below for I<CallSubSV> is identical to I<CallSubPV> except 1102*0Sstevel@tonic-gatethat the C<name> parameter is now defined as an SV* and we use 1103*0Sstevel@tonic-gateI<call_sv> instead of I<call_pv>. 1104*0Sstevel@tonic-gate 1105*0Sstevel@tonic-gate void 1106*0Sstevel@tonic-gate CallSubSV(name) 1107*0Sstevel@tonic-gate SV * name 1108*0Sstevel@tonic-gate CODE: 1109*0Sstevel@tonic-gate PUSHMARK(SP) ; 1110*0Sstevel@tonic-gate call_sv(name, G_DISCARD|G_NOARGS) ; 1111*0Sstevel@tonic-gate 1112*0Sstevel@tonic-gateBecause we are using an SV to call I<fred> the following can all be used 1113*0Sstevel@tonic-gate 1114*0Sstevel@tonic-gate CallSubSV("fred") ; 1115*0Sstevel@tonic-gate CallSubSV(\&fred) ; 1116*0Sstevel@tonic-gate $ref = \&fred ; 1117*0Sstevel@tonic-gate CallSubSV($ref) ; 1118*0Sstevel@tonic-gate CallSubSV( sub { print "Hello there\n" } ) ; 1119*0Sstevel@tonic-gate 1120*0Sstevel@tonic-gateAs you can see, I<call_sv> gives you much greater flexibility in 1121*0Sstevel@tonic-gatehow you can specify the Perl subroutine. 1122*0Sstevel@tonic-gate 1123*0Sstevel@tonic-gateYou should note that if it is necessary to store the SV (C<name> in the 1124*0Sstevel@tonic-gateexample above) which corresponds to the Perl subroutine so that it can 1125*0Sstevel@tonic-gatebe used later in the program, it not enough just to store a copy of the 1126*0Sstevel@tonic-gatepointer to the SV. Say the code above had been like this 1127*0Sstevel@tonic-gate 1128*0Sstevel@tonic-gate static SV * rememberSub ; 1129*0Sstevel@tonic-gate 1130*0Sstevel@tonic-gate void 1131*0Sstevel@tonic-gate SaveSub1(name) 1132*0Sstevel@tonic-gate SV * name 1133*0Sstevel@tonic-gate CODE: 1134*0Sstevel@tonic-gate rememberSub = name ; 1135*0Sstevel@tonic-gate 1136*0Sstevel@tonic-gate void 1137*0Sstevel@tonic-gate CallSavedSub1() 1138*0Sstevel@tonic-gate CODE: 1139*0Sstevel@tonic-gate PUSHMARK(SP) ; 1140*0Sstevel@tonic-gate call_sv(rememberSub, G_DISCARD|G_NOARGS) ; 1141*0Sstevel@tonic-gate 1142*0Sstevel@tonic-gateThe reason this is wrong is that by the time you come to use the 1143*0Sstevel@tonic-gatepointer C<rememberSub> in C<CallSavedSub1>, it may or may not still refer 1144*0Sstevel@tonic-gateto the Perl subroutine that was recorded in C<SaveSub1>. This is 1145*0Sstevel@tonic-gateparticularly true for these cases 1146*0Sstevel@tonic-gate 1147*0Sstevel@tonic-gate SaveSub1(\&fred) ; 1148*0Sstevel@tonic-gate CallSavedSub1() ; 1149*0Sstevel@tonic-gate 1150*0Sstevel@tonic-gate SaveSub1( sub { print "Hello there\n" } ) ; 1151*0Sstevel@tonic-gate CallSavedSub1() ; 1152*0Sstevel@tonic-gate 1153*0Sstevel@tonic-gateBy the time each of the C<SaveSub1> statements above have been executed, 1154*0Sstevel@tonic-gatethe SV*s which corresponded to the parameters will no longer exist. 1155*0Sstevel@tonic-gateExpect an error message from Perl of the form 1156*0Sstevel@tonic-gate 1157*0Sstevel@tonic-gate Can't use an undefined value as a subroutine reference at ... 1158*0Sstevel@tonic-gate 1159*0Sstevel@tonic-gatefor each of the C<CallSavedSub1> lines. 1160*0Sstevel@tonic-gate 1161*0Sstevel@tonic-gateSimilarly, with this code 1162*0Sstevel@tonic-gate 1163*0Sstevel@tonic-gate $ref = \&fred ; 1164*0Sstevel@tonic-gate SaveSub1($ref) ; 1165*0Sstevel@tonic-gate $ref = 47 ; 1166*0Sstevel@tonic-gate CallSavedSub1() ; 1167*0Sstevel@tonic-gate 1168*0Sstevel@tonic-gateyou can expect one of these messages (which you actually get is dependent on 1169*0Sstevel@tonic-gatethe version of Perl you are using) 1170*0Sstevel@tonic-gate 1171*0Sstevel@tonic-gate Not a CODE reference at ... 1172*0Sstevel@tonic-gate Undefined subroutine &main::47 called ... 1173*0Sstevel@tonic-gate 1174*0Sstevel@tonic-gateThe variable $ref may have referred to the subroutine C<fred> 1175*0Sstevel@tonic-gatewhenever the call to C<SaveSub1> was made but by the time 1176*0Sstevel@tonic-gateC<CallSavedSub1> gets called it now holds the number C<47>. Because we 1177*0Sstevel@tonic-gatesaved only a pointer to the original SV in C<SaveSub1>, any changes to 1178*0Sstevel@tonic-gate$ref will be tracked by the pointer C<rememberSub>. This means that 1179*0Sstevel@tonic-gatewhenever C<CallSavedSub1> gets called, it will attempt to execute the 1180*0Sstevel@tonic-gatecode which is referenced by the SV* C<rememberSub>. In this case 1181*0Sstevel@tonic-gatethough, it now refers to the integer C<47>, so expect Perl to complain 1182*0Sstevel@tonic-gateloudly. 1183*0Sstevel@tonic-gate 1184*0Sstevel@tonic-gateA similar but more subtle problem is illustrated with this code 1185*0Sstevel@tonic-gate 1186*0Sstevel@tonic-gate $ref = \&fred ; 1187*0Sstevel@tonic-gate SaveSub1($ref) ; 1188*0Sstevel@tonic-gate $ref = \&joe ; 1189*0Sstevel@tonic-gate CallSavedSub1() ; 1190*0Sstevel@tonic-gate 1191*0Sstevel@tonic-gateThis time whenever C<CallSavedSub1> get called it will execute the Perl 1192*0Sstevel@tonic-gatesubroutine C<joe> (assuming it exists) rather than C<fred> as was 1193*0Sstevel@tonic-gateoriginally requested in the call to C<SaveSub1>. 1194*0Sstevel@tonic-gate 1195*0Sstevel@tonic-gateTo get around these problems it is necessary to take a full copy of the 1196*0Sstevel@tonic-gateSV. The code below shows C<SaveSub2> modified to do that 1197*0Sstevel@tonic-gate 1198*0Sstevel@tonic-gate static SV * keepSub = (SV*)NULL ; 1199*0Sstevel@tonic-gate 1200*0Sstevel@tonic-gate void 1201*0Sstevel@tonic-gate SaveSub2(name) 1202*0Sstevel@tonic-gate SV * name 1203*0Sstevel@tonic-gate CODE: 1204*0Sstevel@tonic-gate /* Take a copy of the callback */ 1205*0Sstevel@tonic-gate if (keepSub == (SV*)NULL) 1206*0Sstevel@tonic-gate /* First time, so create a new SV */ 1207*0Sstevel@tonic-gate keepSub = newSVsv(name) ; 1208*0Sstevel@tonic-gate else 1209*0Sstevel@tonic-gate /* Been here before, so overwrite */ 1210*0Sstevel@tonic-gate SvSetSV(keepSub, name) ; 1211*0Sstevel@tonic-gate 1212*0Sstevel@tonic-gate void 1213*0Sstevel@tonic-gate CallSavedSub2() 1214*0Sstevel@tonic-gate CODE: 1215*0Sstevel@tonic-gate PUSHMARK(SP) ; 1216*0Sstevel@tonic-gate call_sv(keepSub, G_DISCARD|G_NOARGS) ; 1217*0Sstevel@tonic-gate 1218*0Sstevel@tonic-gateTo avoid creating a new SV every time C<SaveSub2> is called, 1219*0Sstevel@tonic-gatethe function first checks to see if it has been called before. If not, 1220*0Sstevel@tonic-gatethen space for a new SV is allocated and the reference to the Perl 1221*0Sstevel@tonic-gatesubroutine, C<name> is copied to the variable C<keepSub> in one 1222*0Sstevel@tonic-gateoperation using C<newSVsv>. Thereafter, whenever C<SaveSub2> is called 1223*0Sstevel@tonic-gatethe existing SV, C<keepSub>, is overwritten with the new value using 1224*0Sstevel@tonic-gateC<SvSetSV>. 1225*0Sstevel@tonic-gate 1226*0Sstevel@tonic-gate=head2 Using call_argv 1227*0Sstevel@tonic-gate 1228*0Sstevel@tonic-gateHere is a Perl subroutine which prints whatever parameters are passed 1229*0Sstevel@tonic-gateto it. 1230*0Sstevel@tonic-gate 1231*0Sstevel@tonic-gate sub PrintList 1232*0Sstevel@tonic-gate { 1233*0Sstevel@tonic-gate my(@list) = @_ ; 1234*0Sstevel@tonic-gate 1235*0Sstevel@tonic-gate foreach (@list) { print "$_\n" } 1236*0Sstevel@tonic-gate } 1237*0Sstevel@tonic-gate 1238*0Sstevel@tonic-gateand here is an example of I<call_argv> which will call 1239*0Sstevel@tonic-gateI<PrintList>. 1240*0Sstevel@tonic-gate 1241*0Sstevel@tonic-gate static char * words[] = {"alpha", "beta", "gamma", "delta", NULL} ; 1242*0Sstevel@tonic-gate 1243*0Sstevel@tonic-gate static void 1244*0Sstevel@tonic-gate call_PrintList() 1245*0Sstevel@tonic-gate { 1246*0Sstevel@tonic-gate dSP ; 1247*0Sstevel@tonic-gate 1248*0Sstevel@tonic-gate call_argv("PrintList", G_DISCARD, words) ; 1249*0Sstevel@tonic-gate } 1250*0Sstevel@tonic-gate 1251*0Sstevel@tonic-gateNote that it is not necessary to call C<PUSHMARK> in this instance. 1252*0Sstevel@tonic-gateThis is because I<call_argv> will do it for you. 1253*0Sstevel@tonic-gate 1254*0Sstevel@tonic-gate=head2 Using call_method 1255*0Sstevel@tonic-gate 1256*0Sstevel@tonic-gateConsider the following Perl code 1257*0Sstevel@tonic-gate 1258*0Sstevel@tonic-gate { 1259*0Sstevel@tonic-gate package Mine ; 1260*0Sstevel@tonic-gate 1261*0Sstevel@tonic-gate sub new 1262*0Sstevel@tonic-gate { 1263*0Sstevel@tonic-gate my($type) = shift ; 1264*0Sstevel@tonic-gate bless [@_] 1265*0Sstevel@tonic-gate } 1266*0Sstevel@tonic-gate 1267*0Sstevel@tonic-gate sub Display 1268*0Sstevel@tonic-gate { 1269*0Sstevel@tonic-gate my ($self, $index) = @_ ; 1270*0Sstevel@tonic-gate print "$index: $$self[$index]\n" ; 1271*0Sstevel@tonic-gate } 1272*0Sstevel@tonic-gate 1273*0Sstevel@tonic-gate sub PrintID 1274*0Sstevel@tonic-gate { 1275*0Sstevel@tonic-gate my($class) = @_ ; 1276*0Sstevel@tonic-gate print "This is Class $class version 1.0\n" ; 1277*0Sstevel@tonic-gate } 1278*0Sstevel@tonic-gate } 1279*0Sstevel@tonic-gate 1280*0Sstevel@tonic-gateIt implements just a very simple class to manage an array. Apart from 1281*0Sstevel@tonic-gatethe constructor, C<new>, it declares methods, one static and one 1282*0Sstevel@tonic-gatevirtual. The static method, C<PrintID>, prints out simply the class 1283*0Sstevel@tonic-gatename and a version number. The virtual method, C<Display>, prints out a 1284*0Sstevel@tonic-gatesingle element of the array. Here is an all Perl example of using it. 1285*0Sstevel@tonic-gate 1286*0Sstevel@tonic-gate $a = new Mine ('red', 'green', 'blue') ; 1287*0Sstevel@tonic-gate $a->Display(1) ; 1288*0Sstevel@tonic-gate PrintID Mine; 1289*0Sstevel@tonic-gate 1290*0Sstevel@tonic-gatewill print 1291*0Sstevel@tonic-gate 1292*0Sstevel@tonic-gate 1: green 1293*0Sstevel@tonic-gate This is Class Mine version 1.0 1294*0Sstevel@tonic-gate 1295*0Sstevel@tonic-gateCalling a Perl method from C is fairly straightforward. The following 1296*0Sstevel@tonic-gatethings are required 1297*0Sstevel@tonic-gate 1298*0Sstevel@tonic-gate=over 5 1299*0Sstevel@tonic-gate 1300*0Sstevel@tonic-gate=item * 1301*0Sstevel@tonic-gate 1302*0Sstevel@tonic-gatea reference to the object for a virtual method or the name of the class 1303*0Sstevel@tonic-gatefor a static method. 1304*0Sstevel@tonic-gate 1305*0Sstevel@tonic-gate=item * 1306*0Sstevel@tonic-gate 1307*0Sstevel@tonic-gatethe name of the method. 1308*0Sstevel@tonic-gate 1309*0Sstevel@tonic-gate=item * 1310*0Sstevel@tonic-gate 1311*0Sstevel@tonic-gateany other parameters specific to the method. 1312*0Sstevel@tonic-gate 1313*0Sstevel@tonic-gate=back 1314*0Sstevel@tonic-gate 1315*0Sstevel@tonic-gateHere is a simple XSUB which illustrates the mechanics of calling both 1316*0Sstevel@tonic-gatethe C<PrintID> and C<Display> methods from C. 1317*0Sstevel@tonic-gate 1318*0Sstevel@tonic-gate void 1319*0Sstevel@tonic-gate call_Method(ref, method, index) 1320*0Sstevel@tonic-gate SV * ref 1321*0Sstevel@tonic-gate char * method 1322*0Sstevel@tonic-gate int index 1323*0Sstevel@tonic-gate CODE: 1324*0Sstevel@tonic-gate PUSHMARK(SP); 1325*0Sstevel@tonic-gate XPUSHs(ref); 1326*0Sstevel@tonic-gate XPUSHs(sv_2mortal(newSViv(index))) ; 1327*0Sstevel@tonic-gate PUTBACK; 1328*0Sstevel@tonic-gate 1329*0Sstevel@tonic-gate call_method(method, G_DISCARD) ; 1330*0Sstevel@tonic-gate 1331*0Sstevel@tonic-gate void 1332*0Sstevel@tonic-gate call_PrintID(class, method) 1333*0Sstevel@tonic-gate char * class 1334*0Sstevel@tonic-gate char * method 1335*0Sstevel@tonic-gate CODE: 1336*0Sstevel@tonic-gate PUSHMARK(SP); 1337*0Sstevel@tonic-gate XPUSHs(sv_2mortal(newSVpv(class, 0))) ; 1338*0Sstevel@tonic-gate PUTBACK; 1339*0Sstevel@tonic-gate 1340*0Sstevel@tonic-gate call_method(method, G_DISCARD) ; 1341*0Sstevel@tonic-gate 1342*0Sstevel@tonic-gate 1343*0Sstevel@tonic-gateSo the methods C<PrintID> and C<Display> can be invoked like this 1344*0Sstevel@tonic-gate 1345*0Sstevel@tonic-gate $a = new Mine ('red', 'green', 'blue') ; 1346*0Sstevel@tonic-gate call_Method($a, 'Display', 1) ; 1347*0Sstevel@tonic-gate call_PrintID('Mine', 'PrintID') ; 1348*0Sstevel@tonic-gate 1349*0Sstevel@tonic-gateThe only thing to note is that in both the static and virtual methods, 1350*0Sstevel@tonic-gatethe method name is not passed via the stack--it is used as the first 1351*0Sstevel@tonic-gateparameter to I<call_method>. 1352*0Sstevel@tonic-gate 1353*0Sstevel@tonic-gate=head2 Using GIMME_V 1354*0Sstevel@tonic-gate 1355*0Sstevel@tonic-gateHere is a trivial XSUB which prints the context in which it is 1356*0Sstevel@tonic-gatecurrently executing. 1357*0Sstevel@tonic-gate 1358*0Sstevel@tonic-gate void 1359*0Sstevel@tonic-gate PrintContext() 1360*0Sstevel@tonic-gate CODE: 1361*0Sstevel@tonic-gate I32 gimme = GIMME_V; 1362*0Sstevel@tonic-gate if (gimme == G_VOID) 1363*0Sstevel@tonic-gate printf ("Context is Void\n") ; 1364*0Sstevel@tonic-gate else if (gimme == G_SCALAR) 1365*0Sstevel@tonic-gate printf ("Context is Scalar\n") ; 1366*0Sstevel@tonic-gate else 1367*0Sstevel@tonic-gate printf ("Context is Array\n") ; 1368*0Sstevel@tonic-gate 1369*0Sstevel@tonic-gateand here is some Perl to test it 1370*0Sstevel@tonic-gate 1371*0Sstevel@tonic-gate PrintContext ; 1372*0Sstevel@tonic-gate $a = PrintContext ; 1373*0Sstevel@tonic-gate @a = PrintContext ; 1374*0Sstevel@tonic-gate 1375*0Sstevel@tonic-gateThe output from that will be 1376*0Sstevel@tonic-gate 1377*0Sstevel@tonic-gate Context is Void 1378*0Sstevel@tonic-gate Context is Scalar 1379*0Sstevel@tonic-gate Context is Array 1380*0Sstevel@tonic-gate 1381*0Sstevel@tonic-gate=head2 Using Perl to dispose of temporaries 1382*0Sstevel@tonic-gate 1383*0Sstevel@tonic-gateIn the examples given to date, any temporaries created in the callback 1384*0Sstevel@tonic-gate(i.e., parameters passed on the stack to the I<call_*> function or 1385*0Sstevel@tonic-gatevalues returned via the stack) have been freed by one of these methods 1386*0Sstevel@tonic-gate 1387*0Sstevel@tonic-gate=over 5 1388*0Sstevel@tonic-gate 1389*0Sstevel@tonic-gate=item * 1390*0Sstevel@tonic-gate 1391*0Sstevel@tonic-gatespecifying the G_DISCARD flag with I<call_*>. 1392*0Sstevel@tonic-gate 1393*0Sstevel@tonic-gate=item * 1394*0Sstevel@tonic-gate 1395*0Sstevel@tonic-gateexplicitly disposed of using the C<ENTER>/C<SAVETMPS> - 1396*0Sstevel@tonic-gateC<FREETMPS>/C<LEAVE> pairing. 1397*0Sstevel@tonic-gate 1398*0Sstevel@tonic-gate=back 1399*0Sstevel@tonic-gate 1400*0Sstevel@tonic-gateThere is another method which can be used, namely letting Perl do it 1401*0Sstevel@tonic-gatefor you automatically whenever it regains control after the callback 1402*0Sstevel@tonic-gatehas terminated. This is done by simply not using the 1403*0Sstevel@tonic-gate 1404*0Sstevel@tonic-gate ENTER ; 1405*0Sstevel@tonic-gate SAVETMPS ; 1406*0Sstevel@tonic-gate ... 1407*0Sstevel@tonic-gate FREETMPS ; 1408*0Sstevel@tonic-gate LEAVE ; 1409*0Sstevel@tonic-gate 1410*0Sstevel@tonic-gatesequence in the callback (and not, of course, specifying the G_DISCARD 1411*0Sstevel@tonic-gateflag). 1412*0Sstevel@tonic-gate 1413*0Sstevel@tonic-gateIf you are going to use this method you have to be aware of a possible 1414*0Sstevel@tonic-gatememory leak which can arise under very specific circumstances. To 1415*0Sstevel@tonic-gateexplain these circumstances you need to know a bit about the flow of 1416*0Sstevel@tonic-gatecontrol between Perl and the callback routine. 1417*0Sstevel@tonic-gate 1418*0Sstevel@tonic-gateThe examples given at the start of the document (an error handler and 1419*0Sstevel@tonic-gatean event driven program) are typical of the two main sorts of flow 1420*0Sstevel@tonic-gatecontrol that you are likely to encounter with callbacks. There is a 1421*0Sstevel@tonic-gatevery important distinction between them, so pay attention. 1422*0Sstevel@tonic-gate 1423*0Sstevel@tonic-gateIn the first example, an error handler, the flow of control could be as 1424*0Sstevel@tonic-gatefollows. You have created an interface to an external library. 1425*0Sstevel@tonic-gateControl can reach the external library like this 1426*0Sstevel@tonic-gate 1427*0Sstevel@tonic-gate perl --> XSUB --> external library 1428*0Sstevel@tonic-gate 1429*0Sstevel@tonic-gateWhilst control is in the library, an error condition occurs. You have 1430*0Sstevel@tonic-gatepreviously set up a Perl callback to handle this situation, so it will 1431*0Sstevel@tonic-gateget executed. Once the callback has finished, control will drop back to 1432*0Sstevel@tonic-gatePerl again. Here is what the flow of control will be like in that 1433*0Sstevel@tonic-gatesituation 1434*0Sstevel@tonic-gate 1435*0Sstevel@tonic-gate perl --> XSUB --> external library 1436*0Sstevel@tonic-gate ... 1437*0Sstevel@tonic-gate error occurs 1438*0Sstevel@tonic-gate ... 1439*0Sstevel@tonic-gate external library --> call_* --> perl 1440*0Sstevel@tonic-gate | 1441*0Sstevel@tonic-gate perl <-- XSUB <-- external library <-- call_* <----+ 1442*0Sstevel@tonic-gate 1443*0Sstevel@tonic-gateAfter processing of the error using I<call_*> is completed, 1444*0Sstevel@tonic-gatecontrol reverts back to Perl more or less immediately. 1445*0Sstevel@tonic-gate 1446*0Sstevel@tonic-gateIn the diagram, the further right you go the more deeply nested the 1447*0Sstevel@tonic-gatescope is. It is only when control is back with perl on the extreme 1448*0Sstevel@tonic-gateleft of the diagram that you will have dropped back to the enclosing 1449*0Sstevel@tonic-gatescope and any temporaries you have left hanging around will be freed. 1450*0Sstevel@tonic-gate 1451*0Sstevel@tonic-gateIn the second example, an event driven program, the flow of control 1452*0Sstevel@tonic-gatewill be more like this 1453*0Sstevel@tonic-gate 1454*0Sstevel@tonic-gate perl --> XSUB --> event handler 1455*0Sstevel@tonic-gate ... 1456*0Sstevel@tonic-gate event handler --> call_* --> perl 1457*0Sstevel@tonic-gate | 1458*0Sstevel@tonic-gate event handler <-- call_* <----+ 1459*0Sstevel@tonic-gate ... 1460*0Sstevel@tonic-gate event handler --> call_* --> perl 1461*0Sstevel@tonic-gate | 1462*0Sstevel@tonic-gate event handler <-- call_* <----+ 1463*0Sstevel@tonic-gate ... 1464*0Sstevel@tonic-gate event handler --> call_* --> perl 1465*0Sstevel@tonic-gate | 1466*0Sstevel@tonic-gate event handler <-- call_* <----+ 1467*0Sstevel@tonic-gate 1468*0Sstevel@tonic-gateIn this case the flow of control can consist of only the repeated 1469*0Sstevel@tonic-gatesequence 1470*0Sstevel@tonic-gate 1471*0Sstevel@tonic-gate event handler --> call_* --> perl 1472*0Sstevel@tonic-gate 1473*0Sstevel@tonic-gatefor practically the complete duration of the program. This means that 1474*0Sstevel@tonic-gatecontrol may I<never> drop back to the surrounding scope in Perl at the 1475*0Sstevel@tonic-gateextreme left. 1476*0Sstevel@tonic-gate 1477*0Sstevel@tonic-gateSo what is the big problem? Well, if you are expecting Perl to tidy up 1478*0Sstevel@tonic-gatethose temporaries for you, you might be in for a long wait. For Perl 1479*0Sstevel@tonic-gateto dispose of your temporaries, control must drop back to the 1480*0Sstevel@tonic-gateenclosing scope at some stage. In the event driven scenario that may 1481*0Sstevel@tonic-gatenever happen. This means that as time goes on, your program will 1482*0Sstevel@tonic-gatecreate more and more temporaries, none of which will ever be freed. As 1483*0Sstevel@tonic-gateeach of these temporaries consumes some memory your program will 1484*0Sstevel@tonic-gateeventually consume all the available memory in your system--kapow! 1485*0Sstevel@tonic-gate 1486*0Sstevel@tonic-gateSo here is the bottom line--if you are sure that control will revert 1487*0Sstevel@tonic-gateback to the enclosing Perl scope fairly quickly after the end of your 1488*0Sstevel@tonic-gatecallback, then it isn't absolutely necessary to dispose explicitly of 1489*0Sstevel@tonic-gateany temporaries you may have created. Mind you, if you are at all 1490*0Sstevel@tonic-gateuncertain about what to do, it doesn't do any harm to tidy up anyway. 1491*0Sstevel@tonic-gate 1492*0Sstevel@tonic-gate 1493*0Sstevel@tonic-gate=head2 Strategies for storing Callback Context Information 1494*0Sstevel@tonic-gate 1495*0Sstevel@tonic-gate 1496*0Sstevel@tonic-gatePotentially one of the trickiest problems to overcome when designing a 1497*0Sstevel@tonic-gatecallback interface can be figuring out how to store the mapping between 1498*0Sstevel@tonic-gatethe C callback function and the Perl equivalent. 1499*0Sstevel@tonic-gate 1500*0Sstevel@tonic-gateTo help understand why this can be a real problem first consider how a 1501*0Sstevel@tonic-gatecallback is set up in an all C environment. Typically a C API will 1502*0Sstevel@tonic-gateprovide a function to register a callback. This will expect a pointer 1503*0Sstevel@tonic-gateto a function as one of its parameters. Below is a call to a 1504*0Sstevel@tonic-gatehypothetical function C<register_fatal> which registers the C function 1505*0Sstevel@tonic-gateto get called when a fatal error occurs. 1506*0Sstevel@tonic-gate 1507*0Sstevel@tonic-gate register_fatal(cb1) ; 1508*0Sstevel@tonic-gate 1509*0Sstevel@tonic-gateThe single parameter C<cb1> is a pointer to a function, so you must 1510*0Sstevel@tonic-gatehave defined C<cb1> in your code, say something like this 1511*0Sstevel@tonic-gate 1512*0Sstevel@tonic-gate static void 1513*0Sstevel@tonic-gate cb1() 1514*0Sstevel@tonic-gate { 1515*0Sstevel@tonic-gate printf ("Fatal Error\n") ; 1516*0Sstevel@tonic-gate exit(1) ; 1517*0Sstevel@tonic-gate } 1518*0Sstevel@tonic-gate 1519*0Sstevel@tonic-gateNow change that to call a Perl subroutine instead 1520*0Sstevel@tonic-gate 1521*0Sstevel@tonic-gate static SV * callback = (SV*)NULL; 1522*0Sstevel@tonic-gate 1523*0Sstevel@tonic-gate static void 1524*0Sstevel@tonic-gate cb1() 1525*0Sstevel@tonic-gate { 1526*0Sstevel@tonic-gate dSP ; 1527*0Sstevel@tonic-gate 1528*0Sstevel@tonic-gate PUSHMARK(SP) ; 1529*0Sstevel@tonic-gate 1530*0Sstevel@tonic-gate /* Call the Perl sub to process the callback */ 1531*0Sstevel@tonic-gate call_sv(callback, G_DISCARD) ; 1532*0Sstevel@tonic-gate } 1533*0Sstevel@tonic-gate 1534*0Sstevel@tonic-gate 1535*0Sstevel@tonic-gate void 1536*0Sstevel@tonic-gate register_fatal(fn) 1537*0Sstevel@tonic-gate SV * fn 1538*0Sstevel@tonic-gate CODE: 1539*0Sstevel@tonic-gate /* Remember the Perl sub */ 1540*0Sstevel@tonic-gate if (callback == (SV*)NULL) 1541*0Sstevel@tonic-gate callback = newSVsv(fn) ; 1542*0Sstevel@tonic-gate else 1543*0Sstevel@tonic-gate SvSetSV(callback, fn) ; 1544*0Sstevel@tonic-gate 1545*0Sstevel@tonic-gate /* register the callback with the external library */ 1546*0Sstevel@tonic-gate register_fatal(cb1) ; 1547*0Sstevel@tonic-gate 1548*0Sstevel@tonic-gatewhere the Perl equivalent of C<register_fatal> and the callback it 1549*0Sstevel@tonic-gateregisters, C<pcb1>, might look like this 1550*0Sstevel@tonic-gate 1551*0Sstevel@tonic-gate # Register the sub pcb1 1552*0Sstevel@tonic-gate register_fatal(\&pcb1) ; 1553*0Sstevel@tonic-gate 1554*0Sstevel@tonic-gate sub pcb1 1555*0Sstevel@tonic-gate { 1556*0Sstevel@tonic-gate die "I'm dying...\n" ; 1557*0Sstevel@tonic-gate } 1558*0Sstevel@tonic-gate 1559*0Sstevel@tonic-gateThe mapping between the C callback and the Perl equivalent is stored in 1560*0Sstevel@tonic-gatethe global variable C<callback>. 1561*0Sstevel@tonic-gate 1562*0Sstevel@tonic-gateThis will be adequate if you ever need to have only one callback 1563*0Sstevel@tonic-gateregistered at any time. An example could be an error handler like the 1564*0Sstevel@tonic-gatecode sketched out above. Remember though, repeated calls to 1565*0Sstevel@tonic-gateC<register_fatal> will replace the previously registered callback 1566*0Sstevel@tonic-gatefunction with the new one. 1567*0Sstevel@tonic-gate 1568*0Sstevel@tonic-gateSay for example you want to interface to a library which allows asynchronous 1569*0Sstevel@tonic-gatefile i/o. In this case you may be able to register a callback whenever 1570*0Sstevel@tonic-gatea read operation has completed. To be of any use we want to be able to 1571*0Sstevel@tonic-gatecall separate Perl subroutines for each file that is opened. As it 1572*0Sstevel@tonic-gatestands, the error handler example above would not be adequate as it 1573*0Sstevel@tonic-gateallows only a single callback to be defined at any time. What we 1574*0Sstevel@tonic-gaterequire is a means of storing the mapping between the opened file and 1575*0Sstevel@tonic-gatethe Perl subroutine we want to be called for that file. 1576*0Sstevel@tonic-gate 1577*0Sstevel@tonic-gateSay the i/o library has a function C<asynch_read> which associates a C 1578*0Sstevel@tonic-gatefunction C<ProcessRead> with a file handle C<fh>--this assumes that it 1579*0Sstevel@tonic-gatehas also provided some routine to open the file and so obtain the file 1580*0Sstevel@tonic-gatehandle. 1581*0Sstevel@tonic-gate 1582*0Sstevel@tonic-gate asynch_read(fh, ProcessRead) 1583*0Sstevel@tonic-gate 1584*0Sstevel@tonic-gateThis may expect the C I<ProcessRead> function of this form 1585*0Sstevel@tonic-gate 1586*0Sstevel@tonic-gate void 1587*0Sstevel@tonic-gate ProcessRead(fh, buffer) 1588*0Sstevel@tonic-gate int fh ; 1589*0Sstevel@tonic-gate char * buffer ; 1590*0Sstevel@tonic-gate { 1591*0Sstevel@tonic-gate ... 1592*0Sstevel@tonic-gate } 1593*0Sstevel@tonic-gate 1594*0Sstevel@tonic-gateTo provide a Perl interface to this library we need to be able to map 1595*0Sstevel@tonic-gatebetween the C<fh> parameter and the Perl subroutine we want called. A 1596*0Sstevel@tonic-gatehash is a convenient mechanism for storing this mapping. The code 1597*0Sstevel@tonic-gatebelow shows a possible implementation 1598*0Sstevel@tonic-gate 1599*0Sstevel@tonic-gate static HV * Mapping = (HV*)NULL ; 1600*0Sstevel@tonic-gate 1601*0Sstevel@tonic-gate void 1602*0Sstevel@tonic-gate asynch_read(fh, callback) 1603*0Sstevel@tonic-gate int fh 1604*0Sstevel@tonic-gate SV * callback 1605*0Sstevel@tonic-gate CODE: 1606*0Sstevel@tonic-gate /* If the hash doesn't already exist, create it */ 1607*0Sstevel@tonic-gate if (Mapping == (HV*)NULL) 1608*0Sstevel@tonic-gate Mapping = newHV() ; 1609*0Sstevel@tonic-gate 1610*0Sstevel@tonic-gate /* Save the fh -> callback mapping */ 1611*0Sstevel@tonic-gate hv_store(Mapping, (char*)&fh, sizeof(fh), newSVsv(callback), 0) ; 1612*0Sstevel@tonic-gate 1613*0Sstevel@tonic-gate /* Register with the C Library */ 1614*0Sstevel@tonic-gate asynch_read(fh, asynch_read_if) ; 1615*0Sstevel@tonic-gate 1616*0Sstevel@tonic-gateand C<asynch_read_if> could look like this 1617*0Sstevel@tonic-gate 1618*0Sstevel@tonic-gate static void 1619*0Sstevel@tonic-gate asynch_read_if(fh, buffer) 1620*0Sstevel@tonic-gate int fh ; 1621*0Sstevel@tonic-gate char * buffer ; 1622*0Sstevel@tonic-gate { 1623*0Sstevel@tonic-gate dSP ; 1624*0Sstevel@tonic-gate SV ** sv ; 1625*0Sstevel@tonic-gate 1626*0Sstevel@tonic-gate /* Get the callback associated with fh */ 1627*0Sstevel@tonic-gate sv = hv_fetch(Mapping, (char*)&fh , sizeof(fh), FALSE) ; 1628*0Sstevel@tonic-gate if (sv == (SV**)NULL) 1629*0Sstevel@tonic-gate croak("Internal error...\n") ; 1630*0Sstevel@tonic-gate 1631*0Sstevel@tonic-gate PUSHMARK(SP) ; 1632*0Sstevel@tonic-gate XPUSHs(sv_2mortal(newSViv(fh))) ; 1633*0Sstevel@tonic-gate XPUSHs(sv_2mortal(newSVpv(buffer, 0))) ; 1634*0Sstevel@tonic-gate PUTBACK ; 1635*0Sstevel@tonic-gate 1636*0Sstevel@tonic-gate /* Call the Perl sub */ 1637*0Sstevel@tonic-gate call_sv(*sv, G_DISCARD) ; 1638*0Sstevel@tonic-gate } 1639*0Sstevel@tonic-gate 1640*0Sstevel@tonic-gateFor completeness, here is C<asynch_close>. This shows how to remove 1641*0Sstevel@tonic-gatethe entry from the hash C<Mapping>. 1642*0Sstevel@tonic-gate 1643*0Sstevel@tonic-gate void 1644*0Sstevel@tonic-gate asynch_close(fh) 1645*0Sstevel@tonic-gate int fh 1646*0Sstevel@tonic-gate CODE: 1647*0Sstevel@tonic-gate /* Remove the entry from the hash */ 1648*0Sstevel@tonic-gate (void) hv_delete(Mapping, (char*)&fh, sizeof(fh), G_DISCARD) ; 1649*0Sstevel@tonic-gate 1650*0Sstevel@tonic-gate /* Now call the real asynch_close */ 1651*0Sstevel@tonic-gate asynch_close(fh) ; 1652*0Sstevel@tonic-gate 1653*0Sstevel@tonic-gateSo the Perl interface would look like this 1654*0Sstevel@tonic-gate 1655*0Sstevel@tonic-gate sub callback1 1656*0Sstevel@tonic-gate { 1657*0Sstevel@tonic-gate my($handle, $buffer) = @_ ; 1658*0Sstevel@tonic-gate } 1659*0Sstevel@tonic-gate 1660*0Sstevel@tonic-gate # Register the Perl callback 1661*0Sstevel@tonic-gate asynch_read($fh, \&callback1) ; 1662*0Sstevel@tonic-gate 1663*0Sstevel@tonic-gate asynch_close($fh) ; 1664*0Sstevel@tonic-gate 1665*0Sstevel@tonic-gateThe mapping between the C callback and Perl is stored in the global 1666*0Sstevel@tonic-gatehash C<Mapping> this time. Using a hash has the distinct advantage that 1667*0Sstevel@tonic-gateit allows an unlimited number of callbacks to be registered. 1668*0Sstevel@tonic-gate 1669*0Sstevel@tonic-gateWhat if the interface provided by the C callback doesn't contain a 1670*0Sstevel@tonic-gateparameter which allows the file handle to Perl subroutine mapping? Say 1671*0Sstevel@tonic-gatein the asynchronous i/o package, the callback function gets passed only 1672*0Sstevel@tonic-gatethe C<buffer> parameter like this 1673*0Sstevel@tonic-gate 1674*0Sstevel@tonic-gate void 1675*0Sstevel@tonic-gate ProcessRead(buffer) 1676*0Sstevel@tonic-gate char * buffer ; 1677*0Sstevel@tonic-gate { 1678*0Sstevel@tonic-gate ... 1679*0Sstevel@tonic-gate } 1680*0Sstevel@tonic-gate 1681*0Sstevel@tonic-gateWithout the file handle there is no straightforward way to map from the 1682*0Sstevel@tonic-gateC callback to the Perl subroutine. 1683*0Sstevel@tonic-gate 1684*0Sstevel@tonic-gateIn this case a possible way around this problem is to predefine a 1685*0Sstevel@tonic-gateseries of C functions to act as the interface to Perl, thus 1686*0Sstevel@tonic-gate 1687*0Sstevel@tonic-gate #define MAX_CB 3 1688*0Sstevel@tonic-gate #define NULL_HANDLE -1 1689*0Sstevel@tonic-gate typedef void (*FnMap)() ; 1690*0Sstevel@tonic-gate 1691*0Sstevel@tonic-gate struct MapStruct { 1692*0Sstevel@tonic-gate FnMap Function ; 1693*0Sstevel@tonic-gate SV * PerlSub ; 1694*0Sstevel@tonic-gate int Handle ; 1695*0Sstevel@tonic-gate } ; 1696*0Sstevel@tonic-gate 1697*0Sstevel@tonic-gate static void fn1() ; 1698*0Sstevel@tonic-gate static void fn2() ; 1699*0Sstevel@tonic-gate static void fn3() ; 1700*0Sstevel@tonic-gate 1701*0Sstevel@tonic-gate static struct MapStruct Map [MAX_CB] = 1702*0Sstevel@tonic-gate { 1703*0Sstevel@tonic-gate { fn1, NULL, NULL_HANDLE }, 1704*0Sstevel@tonic-gate { fn2, NULL, NULL_HANDLE }, 1705*0Sstevel@tonic-gate { fn3, NULL, NULL_HANDLE } 1706*0Sstevel@tonic-gate } ; 1707*0Sstevel@tonic-gate 1708*0Sstevel@tonic-gate static void 1709*0Sstevel@tonic-gate Pcb(index, buffer) 1710*0Sstevel@tonic-gate int index ; 1711*0Sstevel@tonic-gate char * buffer ; 1712*0Sstevel@tonic-gate { 1713*0Sstevel@tonic-gate dSP ; 1714*0Sstevel@tonic-gate 1715*0Sstevel@tonic-gate PUSHMARK(SP) ; 1716*0Sstevel@tonic-gate XPUSHs(sv_2mortal(newSVpv(buffer, 0))) ; 1717*0Sstevel@tonic-gate PUTBACK ; 1718*0Sstevel@tonic-gate 1719*0Sstevel@tonic-gate /* Call the Perl sub */ 1720*0Sstevel@tonic-gate call_sv(Map[index].PerlSub, G_DISCARD) ; 1721*0Sstevel@tonic-gate } 1722*0Sstevel@tonic-gate 1723*0Sstevel@tonic-gate static void 1724*0Sstevel@tonic-gate fn1(buffer) 1725*0Sstevel@tonic-gate char * buffer ; 1726*0Sstevel@tonic-gate { 1727*0Sstevel@tonic-gate Pcb(0, buffer) ; 1728*0Sstevel@tonic-gate } 1729*0Sstevel@tonic-gate 1730*0Sstevel@tonic-gate static void 1731*0Sstevel@tonic-gate fn2(buffer) 1732*0Sstevel@tonic-gate char * buffer ; 1733*0Sstevel@tonic-gate { 1734*0Sstevel@tonic-gate Pcb(1, buffer) ; 1735*0Sstevel@tonic-gate } 1736*0Sstevel@tonic-gate 1737*0Sstevel@tonic-gate static void 1738*0Sstevel@tonic-gate fn3(buffer) 1739*0Sstevel@tonic-gate char * buffer ; 1740*0Sstevel@tonic-gate { 1741*0Sstevel@tonic-gate Pcb(2, buffer) ; 1742*0Sstevel@tonic-gate } 1743*0Sstevel@tonic-gate 1744*0Sstevel@tonic-gate void 1745*0Sstevel@tonic-gate array_asynch_read(fh, callback) 1746*0Sstevel@tonic-gate int fh 1747*0Sstevel@tonic-gate SV * callback 1748*0Sstevel@tonic-gate CODE: 1749*0Sstevel@tonic-gate int index ; 1750*0Sstevel@tonic-gate int null_index = MAX_CB ; 1751*0Sstevel@tonic-gate 1752*0Sstevel@tonic-gate /* Find the same handle or an empty entry */ 1753*0Sstevel@tonic-gate for (index = 0 ; index < MAX_CB ; ++index) 1754*0Sstevel@tonic-gate { 1755*0Sstevel@tonic-gate if (Map[index].Handle == fh) 1756*0Sstevel@tonic-gate break ; 1757*0Sstevel@tonic-gate 1758*0Sstevel@tonic-gate if (Map[index].Handle == NULL_HANDLE) 1759*0Sstevel@tonic-gate null_index = index ; 1760*0Sstevel@tonic-gate } 1761*0Sstevel@tonic-gate 1762*0Sstevel@tonic-gate if (index == MAX_CB && null_index == MAX_CB) 1763*0Sstevel@tonic-gate croak ("Too many callback functions registered\n") ; 1764*0Sstevel@tonic-gate 1765*0Sstevel@tonic-gate if (index == MAX_CB) 1766*0Sstevel@tonic-gate index = null_index ; 1767*0Sstevel@tonic-gate 1768*0Sstevel@tonic-gate /* Save the file handle */ 1769*0Sstevel@tonic-gate Map[index].Handle = fh ; 1770*0Sstevel@tonic-gate 1771*0Sstevel@tonic-gate /* Remember the Perl sub */ 1772*0Sstevel@tonic-gate if (Map[index].PerlSub == (SV*)NULL) 1773*0Sstevel@tonic-gate Map[index].PerlSub = newSVsv(callback) ; 1774*0Sstevel@tonic-gate else 1775*0Sstevel@tonic-gate SvSetSV(Map[index].PerlSub, callback) ; 1776*0Sstevel@tonic-gate 1777*0Sstevel@tonic-gate asynch_read(fh, Map[index].Function) ; 1778*0Sstevel@tonic-gate 1779*0Sstevel@tonic-gate void 1780*0Sstevel@tonic-gate array_asynch_close(fh) 1781*0Sstevel@tonic-gate int fh 1782*0Sstevel@tonic-gate CODE: 1783*0Sstevel@tonic-gate int index ; 1784*0Sstevel@tonic-gate 1785*0Sstevel@tonic-gate /* Find the file handle */ 1786*0Sstevel@tonic-gate for (index = 0; index < MAX_CB ; ++ index) 1787*0Sstevel@tonic-gate if (Map[index].Handle == fh) 1788*0Sstevel@tonic-gate break ; 1789*0Sstevel@tonic-gate 1790*0Sstevel@tonic-gate if (index == MAX_CB) 1791*0Sstevel@tonic-gate croak ("could not close fh %d\n", fh) ; 1792*0Sstevel@tonic-gate 1793*0Sstevel@tonic-gate Map[index].Handle = NULL_HANDLE ; 1794*0Sstevel@tonic-gate SvREFCNT_dec(Map[index].PerlSub) ; 1795*0Sstevel@tonic-gate Map[index].PerlSub = (SV*)NULL ; 1796*0Sstevel@tonic-gate 1797*0Sstevel@tonic-gate asynch_close(fh) ; 1798*0Sstevel@tonic-gate 1799*0Sstevel@tonic-gateIn this case the functions C<fn1>, C<fn2>, and C<fn3> are used to 1800*0Sstevel@tonic-gateremember the Perl subroutine to be called. Each of the functions holds 1801*0Sstevel@tonic-gatea separate hard-wired index which is used in the function C<Pcb> to 1802*0Sstevel@tonic-gateaccess the C<Map> array and actually call the Perl subroutine. 1803*0Sstevel@tonic-gate 1804*0Sstevel@tonic-gateThere are some obvious disadvantages with this technique. 1805*0Sstevel@tonic-gate 1806*0Sstevel@tonic-gateFirstly, the code is considerably more complex than with the previous 1807*0Sstevel@tonic-gateexample. 1808*0Sstevel@tonic-gate 1809*0Sstevel@tonic-gateSecondly, there is a hard-wired limit (in this case 3) to the number of 1810*0Sstevel@tonic-gatecallbacks that can exist simultaneously. The only way to increase the 1811*0Sstevel@tonic-gatelimit is by modifying the code to add more functions and then 1812*0Sstevel@tonic-gaterecompiling. None the less, as long as the number of functions is 1813*0Sstevel@tonic-gatechosen with some care, it is still a workable solution and in some 1814*0Sstevel@tonic-gatecases is the only one available. 1815*0Sstevel@tonic-gate 1816*0Sstevel@tonic-gateTo summarize, here are a number of possible methods for you to consider 1817*0Sstevel@tonic-gatefor storing the mapping between C and the Perl callback 1818*0Sstevel@tonic-gate 1819*0Sstevel@tonic-gate=over 5 1820*0Sstevel@tonic-gate 1821*0Sstevel@tonic-gate=item 1. Ignore the problem - Allow only 1 callback 1822*0Sstevel@tonic-gate 1823*0Sstevel@tonic-gateFor a lot of situations, like interfacing to an error handler, this may 1824*0Sstevel@tonic-gatebe a perfectly adequate solution. 1825*0Sstevel@tonic-gate 1826*0Sstevel@tonic-gate=item 2. Create a sequence of callbacks - hard wired limit 1827*0Sstevel@tonic-gate 1828*0Sstevel@tonic-gateIf it is impossible to tell from the parameters passed back from the C 1829*0Sstevel@tonic-gatecallback what the context is, then you may need to create a sequence of C 1830*0Sstevel@tonic-gatecallback interface functions, and store pointers to each in an array. 1831*0Sstevel@tonic-gate 1832*0Sstevel@tonic-gate=item 3. Use a parameter to map to the Perl callback 1833*0Sstevel@tonic-gate 1834*0Sstevel@tonic-gateA hash is an ideal mechanism to store the mapping between C and Perl. 1835*0Sstevel@tonic-gate 1836*0Sstevel@tonic-gate=back 1837*0Sstevel@tonic-gate 1838*0Sstevel@tonic-gate 1839*0Sstevel@tonic-gate=head2 Alternate Stack Manipulation 1840*0Sstevel@tonic-gate 1841*0Sstevel@tonic-gate 1842*0Sstevel@tonic-gateAlthough I have made use of only the C<POP*> macros to access values 1843*0Sstevel@tonic-gatereturned from Perl subroutines, it is also possible to bypass these 1844*0Sstevel@tonic-gatemacros and read the stack using the C<ST> macro (See L<perlxs> for a 1845*0Sstevel@tonic-gatefull description of the C<ST> macro). 1846*0Sstevel@tonic-gate 1847*0Sstevel@tonic-gateMost of the time the C<POP*> macros should be adequate, the main 1848*0Sstevel@tonic-gateproblem with them is that they force you to process the returned values 1849*0Sstevel@tonic-gatein sequence. This may not be the most suitable way to process the 1850*0Sstevel@tonic-gatevalues in some cases. What we want is to be able to access the stack in 1851*0Sstevel@tonic-gatea random order. The C<ST> macro as used when coding an XSUB is ideal 1852*0Sstevel@tonic-gatefor this purpose. 1853*0Sstevel@tonic-gate 1854*0Sstevel@tonic-gateThe code below is the example given in the section I<Returning a list 1855*0Sstevel@tonic-gateof values> recoded to use C<ST> instead of C<POP*>. 1856*0Sstevel@tonic-gate 1857*0Sstevel@tonic-gate static void 1858*0Sstevel@tonic-gate call_AddSubtract2(a, b) 1859*0Sstevel@tonic-gate int a ; 1860*0Sstevel@tonic-gate int b ; 1861*0Sstevel@tonic-gate { 1862*0Sstevel@tonic-gate dSP ; 1863*0Sstevel@tonic-gate I32 ax ; 1864*0Sstevel@tonic-gate int count ; 1865*0Sstevel@tonic-gate 1866*0Sstevel@tonic-gate ENTER ; 1867*0Sstevel@tonic-gate SAVETMPS; 1868*0Sstevel@tonic-gate 1869*0Sstevel@tonic-gate PUSHMARK(SP) ; 1870*0Sstevel@tonic-gate XPUSHs(sv_2mortal(newSViv(a))); 1871*0Sstevel@tonic-gate XPUSHs(sv_2mortal(newSViv(b))); 1872*0Sstevel@tonic-gate PUTBACK ; 1873*0Sstevel@tonic-gate 1874*0Sstevel@tonic-gate count = call_pv("AddSubtract", G_ARRAY); 1875*0Sstevel@tonic-gate 1876*0Sstevel@tonic-gate SPAGAIN ; 1877*0Sstevel@tonic-gate SP -= count ; 1878*0Sstevel@tonic-gate ax = (SP - PL_stack_base) + 1 ; 1879*0Sstevel@tonic-gate 1880*0Sstevel@tonic-gate if (count != 2) 1881*0Sstevel@tonic-gate croak("Big trouble\n") ; 1882*0Sstevel@tonic-gate 1883*0Sstevel@tonic-gate printf ("%d + %d = %d\n", a, b, SvIV(ST(0))) ; 1884*0Sstevel@tonic-gate printf ("%d - %d = %d\n", a, b, SvIV(ST(1))) ; 1885*0Sstevel@tonic-gate 1886*0Sstevel@tonic-gate PUTBACK ; 1887*0Sstevel@tonic-gate FREETMPS ; 1888*0Sstevel@tonic-gate LEAVE ; 1889*0Sstevel@tonic-gate } 1890*0Sstevel@tonic-gate 1891*0Sstevel@tonic-gateNotes 1892*0Sstevel@tonic-gate 1893*0Sstevel@tonic-gate=over 5 1894*0Sstevel@tonic-gate 1895*0Sstevel@tonic-gate=item 1. 1896*0Sstevel@tonic-gate 1897*0Sstevel@tonic-gateNotice that it was necessary to define the variable C<ax>. This is 1898*0Sstevel@tonic-gatebecause the C<ST> macro expects it to exist. If we were in an XSUB it 1899*0Sstevel@tonic-gatewould not be necessary to define C<ax> as it is already defined for 1900*0Sstevel@tonic-gateyou. 1901*0Sstevel@tonic-gate 1902*0Sstevel@tonic-gate=item 2. 1903*0Sstevel@tonic-gate 1904*0Sstevel@tonic-gateThe code 1905*0Sstevel@tonic-gate 1906*0Sstevel@tonic-gate SPAGAIN ; 1907*0Sstevel@tonic-gate SP -= count ; 1908*0Sstevel@tonic-gate ax = (SP - PL_stack_base) + 1 ; 1909*0Sstevel@tonic-gate 1910*0Sstevel@tonic-gatesets the stack up so that we can use the C<ST> macro. 1911*0Sstevel@tonic-gate 1912*0Sstevel@tonic-gate=item 3. 1913*0Sstevel@tonic-gate 1914*0Sstevel@tonic-gateUnlike the original coding of this example, the returned 1915*0Sstevel@tonic-gatevalues are not accessed in reverse order. So C<ST(0)> refers to the 1916*0Sstevel@tonic-gatefirst value returned by the Perl subroutine and C<ST(count-1)> 1917*0Sstevel@tonic-gaterefers to the last. 1918*0Sstevel@tonic-gate 1919*0Sstevel@tonic-gate=back 1920*0Sstevel@tonic-gate 1921*0Sstevel@tonic-gate=head2 Creating and calling an anonymous subroutine in C 1922*0Sstevel@tonic-gate 1923*0Sstevel@tonic-gateAs we've already shown, C<call_sv> can be used to invoke an 1924*0Sstevel@tonic-gateanonymous subroutine. However, our example showed a Perl script 1925*0Sstevel@tonic-gateinvoking an XSUB to perform this operation. Let's see how it can be 1926*0Sstevel@tonic-gatedone inside our C code: 1927*0Sstevel@tonic-gate 1928*0Sstevel@tonic-gate ... 1929*0Sstevel@tonic-gate 1930*0Sstevel@tonic-gate SV *cvrv = eval_pv("sub { print 'You will not find me cluttering any namespace!' }", TRUE); 1931*0Sstevel@tonic-gate 1932*0Sstevel@tonic-gate ... 1933*0Sstevel@tonic-gate 1934*0Sstevel@tonic-gate call_sv(cvrv, G_VOID|G_NOARGS); 1935*0Sstevel@tonic-gate 1936*0Sstevel@tonic-gateC<eval_pv> is used to compile the anonymous subroutine, which 1937*0Sstevel@tonic-gatewill be the return value as well (read more about C<eval_pv> in 1938*0Sstevel@tonic-gateL<perlapi/eval_pv>). Once this code reference is in hand, it 1939*0Sstevel@tonic-gatecan be mixed in with all the previous examples we've shown. 1940*0Sstevel@tonic-gate 1941*0Sstevel@tonic-gate=head1 SEE ALSO 1942*0Sstevel@tonic-gate 1943*0Sstevel@tonic-gateL<perlxs>, L<perlguts>, L<perlembed> 1944*0Sstevel@tonic-gate 1945*0Sstevel@tonic-gate=head1 AUTHOR 1946*0Sstevel@tonic-gate 1947*0Sstevel@tonic-gatePaul Marquess 1948*0Sstevel@tonic-gate 1949*0Sstevel@tonic-gateSpecial thanks to the following people who assisted in the creation of 1950*0Sstevel@tonic-gatethe document. 1951*0Sstevel@tonic-gate 1952*0Sstevel@tonic-gateJeff Okamoto, Tim Bunce, Nick Gianniotis, Steve Kelem, Gurusamy Sarathy 1953*0Sstevel@tonic-gateand Larry Wall. 1954*0Sstevel@tonic-gate 1955*0Sstevel@tonic-gate=head1 DATE 1956*0Sstevel@tonic-gate 1957*0Sstevel@tonic-gateVersion 1.3, 14th Apr 1997 1958