home *** CD-ROM | disk | FTP | other *** search
Text File | 1995-07-26 | 39.3 KB | 1,057 lines |
-
-
-
- PPPPEEEERRRRLLLLCCCCAAAALLLLLLLL((((1111)))) UUUUNNNNIIIIXXXX SSSSyyyysssstttteeeemmmm VVVV ((((RRRReeeelllleeeeaaaasssseeee 0000....0000 PPPPaaaattttcccchhhhlllleeeevvvveeeellll 00000000)))) PPPPEEEERRRRLLLLCCCCAAAALLLLLLLL((((1111))))
-
-
-
- NNNNAAAAMMMMEEEE
- perlcall - Perl calling conventions from C
-
- DDDDEEEESSSSCCCCRRRRIIIIPPPPTTTTIIIIOOOONNNN
- WWWWAAAARRRRNNNNIIIINNNNGGGG :::: TTTThhhhiiiissss ddddooooccccuuuummmmeeeennnntttt iiiissss ssssttttiiiillllllll uuuunnnnddddeeeerrrr ccccoooonnnnssssttttrrrruuuuccccttttiiiioooonnnn.... TTTThhhheeeerrrreeee
- aaaarrrreeee bbbboooouuuunnnndddd ttttoooo bbbbeeee aaaa nnnnuuuummmmbbbbeeeerrrr ooooffff iiiinnnnaaaaccccccccuuuurrrraaaacccciiiieeeessss,,,, ssssoooo ttttrrrreeeeaaaadddd vvvveeeerrrryyyy
- ccccaaaarrrreeeeffffuuuullllllllyyyy ffffoooorrrr nnnnoooowwww....
-
- The purpose of this document is to show you how to write
- _c_a_l_l_b_a_c_k_s, i.e. how to call Perl from C. The main focus is
- on how to interface back to Perl from a bit of C code that
- has itself been run by Perl, i.e. the 'main' program is a
- Perl script; you are using it to execute a section of code
- written in C; that bit of C code wants you to do something
- with a particular event, so you want a Perl sub to be
- executed whenever it happens.
-
- Examples where this is necessary include
-
- o+ You have created an XSUB interface to an application's
- C API.
-
- A fairly common feature in applications is to allow you
- to define a C function that will get called whenever
- something nasty occurs. What we would like is for a
- Perl sub to be called instead.
-
- o+ The classic example of where callbacks are used is in
- an event driven program like for X-windows. In this
- case your register functions to be called whenever a
- specific events occur, e.g. a mouse button is pressed.
-
- Although the techniques described are applicable to
- embedding Perl in a C program, this is not the primary goal
- of this document. For details on embedding Perl in C refer
- to the _p_e_r_l_e_m_b_e_d manpage (currently unwritten).
-
- Before you launch yourself head first into the rest of this
- document, it would be a good idea to have read the following
- two documents - the _p_e_r_l_a_p_i manpage and the _p_e_r_l_g_u_t_s
- manpage.
-
- This stuff is easier to explain using examples. But first
- here are a few definitions anyway.
-
- DDDDeeeeffffiiiinnnniiiittttiiiioooonnnnssss
-
- Perl has a number of C functions which allow you to call
- Perl subs. They are
-
-
-
-
-
-
- Page 1 (printed 6/30/95)
-
-
-
-
-
-
- PPPPEEEERRRRLLLLCCCCAAAALLLLLLLL((((1111)))) UUUUNNNNIIIIXXXX SSSSyyyysssstttteeeemmmm VVVV ((((RRRReeeelllleeeeaaaasssseeee 0000....0000 PPPPaaaattttcccchhhhlllleeeevvvveeeellll 00000000)))) PPPPEEEERRRRLLLLCCCCAAAALLLLLLLL((((1111))))
-
-
-
- I32 perl_call_sv(SV* sv, I32 flags) ;
- I32 perl_call_pv(char *subname, I32 flags) ;
- I32 perl_call_method(char *methname, I32 flags) ;
- I32 perl_call_argv(char *subname, I32 flags, register char **argv) ;
-
- The key function is _p_e_r_l__c_a_l_l__s_v. All the other functions
- make use of _p_e_r_l__c_a_l_l__s_v to do what they do.
-
- _p_e_r_l__c_a_l_l__s_v takes two parameters, the first is an SV*. This
- allows you to specify the Perl sub to be called either as a
- C string (which has first been converted to an SV) or a
- reference to a sub. Example 7, shows you how you can make
- use of _p_e_r_l__c_a_l_l__s_v. The second parameter, flags, is a
- general purpose option command. This parameter is common to
- all the _p_e_r_l__c_a_l_l_* functions. It is discussed in the next
- section.
-
- The function, _p_e_r_l__c_a_l_l__p_v, is similar as _p_e_r_l__c_a_l_l__s_v
- except it expects it's first parameter has to be a C char*
- which identifies the Perl sub you want to call, e.g.
- perl_call_pv("fred", 0).
-
- The function _p_e_r_l__c_a_l_l__m_e_t_h_o_d expects its first argument to
- contain a blessed reference to a class. Using that reference
- it looks up and calls methname from that class. See example
- 9.
-
- _p_e_r_l__c_a_l_l__a_r_g_v calls the Perl sub specified by the subname
- parameter. It also takes the usual flags parameter. The
- final parameter, argv, consists of a list of C strings to be
- sent to the Perl sub. See example 8.
-
- All the functions return a number. This is a count of the
- number of items returned by the Perl sub on the stack.
-
- As a general rule you should _a_l_w_a_y_s check the return value
- from these functions. Even if you are only expecting a
- particular number of values to be returned from the Perl
- sub, there is nothing to stop someone from doing something
- unexpected - don't say you havn't been warned.
-
- FFFFllllaaaagggg VVVVaaaalllluuuueeeessss
-
- The flags parameter in all the _p_e_r_l__c_a_l_l_* functions
- consists of any combination of the symbols defined below,
- OR'ed together.
-
- G_SCALAR
- Calls the Perl sub in a scalar context.
-
- Whatever the Perl sub actually returns, we only want a
- scalar. If the perl sub does return a scalar, the
-
-
-
- Page 2 (printed 6/30/95)
-
-
-
-
-
-
- PPPPEEEERRRRLLLLCCCCAAAALLLLLLLL((((1111)))) UUUUNNNNIIIIXXXX SSSSyyyysssstttteeeemmmm VVVV ((((RRRReeeelllleeeeaaaasssseeee 0000....0000 PPPPaaaattttcccchhhhlllleeeevvvveeeellll 00000000)))) PPPPEEEERRRRLLLLCCCCAAAALLLLLLLL((((1111))))
-
-
-
- return value from the _p_e_r_l__c_a_l_l_* function will be 1 or
- 0. If 1, then the value actually returned by the Perl
- sub will be contained on the top of the stack. If 0,
- then the sub has probably called _d_i_e or you have used
- the G_DISCARD flag.
-
- If the Perl sub returns a list, the _p_e_r_l__c_a_l_l_*
- function will still only return 1 or 0. If 1, then the
- number of elements in the list will be stored on top of
- the stack. The actual values of the list will not be
- accessable.
-
- G_SCALAR is the default flag setting for all the
- functions.
-
- G_ARRAY
- Calls the Perl sub in a list context.
-
- The return code from the _p_e_r_l__c_a_l_l_* functions will
- indicate how many elements of the stack are used to
- store the array.
-
- G_DISCARD
- If you are not interested in the values returned by the
- Perl sub then setting this flag will make Perl get rid
- of them automatically for you. This will take
- precedence to either G_SCALAR or G_ARRAY.
-
- If you do not set this flag then you may need to
- explicitly get rid of temporary values. See example 3
- for details.
-
- G_NOARGS
- If you are not passing any parameters to the Perl sub,
- you can save a bit of time by setting this flag. It has
- the effect of of not creating the @_ array for the Perl
- sub.
-
- A point worth noting is that if this flag is specified
- the Perl sub called can still access an @_ array from a
- previous Perl sub. This functionality can be
- illustrated with the perl code below
-
- sub fred
- { print "@_\n" }
-
- sub joe
- { &fred }
-
- &joe(1,2,3) ;
-
- This will print
-
-
-
- Page 3 (printed 6/30/95)
-
-
-
-
-
-
- PPPPEEEERRRRLLLLCCCCAAAALLLLLLLL((((1111)))) UUUUNNNNIIIIXXXX SSSSyyyysssstttteeeemmmm VVVV ((((RRRReeeelllleeeeaaaasssseeee 0000....0000 PPPPaaaattttcccchhhhlllleeeevvvveeeellll 00000000)))) PPPPEEEERRRRLLLLCCCCAAAALLLLLLLL((((1111))))
-
-
-
- 1 2 3
-
- What has happened is that fred accesses the @_ array
- which belongs to joe.
-
- G_EVAL
- If the Perl sub you are calling has the ability to
- terminate abnormally, e.g. by calling _d_i_e or by not
- actually existing, and you want to catch this type of
- event, specify this flag setting. It will put an _e_v_a_l {
- } around the sub call.
-
- Whenever control returns from the _p_e_r_l__c_a_l_l_* function
- you need to check the $@ variable as you would in a
- normal Perl script. See example 6 for details of how to
- do this.
-
- EEEEXXXXAAAAMMMMPPPPLLLLEEEESSSS
- Enough of the definition talk, let's have a few examples.
-
- Perl provides many macros to assist in accessing the Perl
- stack. These macros should always be used when interfacing
- to Perl internals. Hopefully this should make the code less
- vulnerable to changes made to Perl in the future.
-
- Another point worth noting is that in the first series of
- examples I have only made use of the _p_e_r_l__c_a_l_l__p_v function.
- This has only been done to ease you into the topic. Wherever
- possible, if the choice is between using _p_e_r_l__c_a_l_l__p_v and
- _p_e_r_l__c_a_l_l__s_v, I would always try to use _p_e_r_l__c_a_l_l__s_v.
-
- The code for these examples is stored in the file
- _p_e_r_l_c_a_l_l._t_a_r. (Once this document settles down, all the
- example code will be available in the file).
-
- EEEExxxxaaaammmmpppplllleeee1111:::: NNNNoooo PPPPaaaarrrraaaammmmeeeetttteeeerrrrssss,,,, NNNNooootttthhhhiiiinnnngggg rrrreeeettttuuuurrrrnnnneeeedddd
-
- This first trivial example will call a Perl sub, _P_r_i_n_t_U_I_D,
- to print out the UID of the process.
-
- sub PrintUID
- {
- print "UID is $<\n" ;
- }
-
- and here is the C to call it
-
- void
- call_PrintUID()
- {
- dSP ;
-
-
-
-
- Page 4 (printed 6/30/95)
-
-
-
-
-
-
- PPPPEEEERRRRLLLLCCCCAAAALLLLLLLL((((1111)))) UUUUNNNNIIIIXXXX SSSSyyyysssstttteeeemmmm VVVV ((((RRRReeeelllleeeeaaaasssseeee 0000....0000 PPPPaaaattttcccchhhhlllleeeevvvveeeellll 00000000)))) PPPPEEEERRRRLLLLCCCCAAAALLLLLLLL((((1111))))
-
-
-
- PUSHMARK(sp) ;
- perl_call_pv("PrintUID", G_DISCARD|G_NOARGS) ;
- }
-
- Simple, eh.
-
- A few points to note about this example.
-
- 1. We aren't passing any parameters to _P_r_i_n_t_U_I_D so
- G_NOARGS can be specified.
-
- 2. Ignore dSP and PUSHMARK(sp) for now. They will be
- discussed in the next example.
-
- 3. We aren't interested in anything returned from
- _P_r_i_n_t_U_I_D, so G_DISCARD is specified. Even if _P_r_i_n_t_U_I_D
- was changed to actually return some _v_a_l_u_e(s), having
- specified G_DISCARD will mean that they will be wiped
- by the time control returns from _p_e_r_l__c_a_l_l__p_v.
-
- 4. Because we specified G_DISCARD, it is not necessary to
- check the value returned from _p_e_r_l__c_a_l_l__s_v. It will
- always be 0.
-
- 5. As _p_e_r_l__c_a_l_l__p_v is being used, the Perl sub is
- specified as a C string.
-
- EEEExxxxaaaammmmpppplllleeee 2222:::: PPPPaaaassssssssiiiinnnngggg PPPPaaaarrrraaaammmmeeeetttteeeerrrrssss
-
- Now let's make a slightly more complex example. This time we
- want to call a Perl sub which will take 2 parameters - a
- string ($s) and an integer ($n). The sub will simply print
- the first $n characters of the string.
-
- So the Perl sub would look like this
-
- sub LeftString
- {
- my($s, $n) = @_ ;
- print substr($s, 0, $n), "\n" ;
- }
-
- The C function required to call _L_e_f_t_S_t_r_i_n_g would look like
- this.
-
- static void
- call_LeftString(a, b)
- char * a ;
- int b ;
- {
- dSP ;
-
-
-
-
- Page 5 (printed 6/30/95)
-
-
-
-
-
-
- PPPPEEEERRRRLLLLCCCCAAAALLLLLLLL((((1111)))) UUUUNNNNIIIIXXXX SSSSyyyysssstttteeeemmmm VVVV ((((RRRReeeelllleeeeaaaasssseeee 0000....0000 PPPPaaaattttcccchhhhlllleeeevvvveeeellll 00000000)))) PPPPEEEERRRRLLLLCCCCAAAALLLLLLLL((((1111))))
-
-
-
- PUSHMARK(sp) ;
- XPUSHs(sv_2mortal(newSVpv(a, 0)));
- XPUSHs(sv_2mortal(newSViv(b)));
- PUTBACK ;
-
- perl_call_pv("LeftString", G_DISCARD);
- }
-
- Here are a few notes on the C function _c_a_l_l__L_e_f_t_S_t_r_i_n_g.
-
- 1. The only flag specified this time is G_DISCARD. As we
- are passing 2 parameters to the Perl sub this time, we
- have not specified G_NOARGS.
-
- 2. Parameters are passed to the Perl sub using the Perl
- stack. This is the purpose of the code beginning with
- the line dSP and ending with the line PUTBACK.
-
- 3. If you are going to put something onto the Perl stack,
- you need to know where to put it. This is the purpose
- of the macro dSP - it declares and initialises a local
- copy of the Perl stack pointer.
-
- All the other macros which will be used in this example
- require you to have used this macro.
-
- If you are calling a Perl sub directly from an XSUB
- function, it is not necessary to explicitly use the dSP
- macro - it will be declared for you.
-
- 4. Any parameters to be pushed onto the stack should be
- bracketed by the PUSHMARK and PUTBACK macros. The
- purpose of these two macros, in this context, is to
- automatically count the number of parameters you are
- pushing. Then whenever Perl is creating the @_ array
- for the sub, it knows how big to make it.
-
- The PUSHMARK macro tells Perl to make a mental note of
- the current stack pointer. Even if you aren't passing
- any parameters (like in Example 1) you must still call
- the PUSHMARK macro before you can call any of the
- _p_e_r_l__c_a_l_l_* functions - Perl still needs to know that
- there are no parameters.
-
- The PUTBACK macro sets the global copy of the stack
- pointer to be the same as our local copy. If we didn't
- do this _p_e_r_l__c_a_l_l__p_v wouldn't know where the two
- parameters we pushed were - remember that up to now all
- the stack pointer manipulation we have done is with our
- local copy, _n_o_t the global copy.
-
-
-
-
-
- Page 6 (printed 6/30/95)
-
-
-
-
-
-
- PPPPEEEERRRRLLLLCCCCAAAALLLLLLLL((((1111)))) UUUUNNNNIIIIXXXX SSSSyyyysssstttteeeemmmm VVVV ((((RRRReeeelllleeeeaaaasssseeee 0000....0000 PPPPaaaattttcccchhhhlllleeeevvvveeeellll 00000000)))) PPPPEEEERRRRLLLLCCCCAAAALLLLLLLL((((1111))))
-
-
-
- 5. Next, we come to XPUSHs. This is where the parameters
- actually get pushed onto the stack. In this case we are
- pushing a string and an integer.
-
- See the section _X_S_U_B'_s _A_N_D _T_H_E _A_R_G_U_M_E_N_T _S_T_A_C_K in the
- _p_e_r_l_g_u_t_s manpage for details on how the XPUSH macros
- work.
-
- 6. Finally, _L_e_f_t_S_t_r_i_n_g can now be called via the
- _p_e_r_l__c_a_l_l__p_v function.
-
- EEEExxxxaaaammmmpppplllleeee 3333:::: RRRReeeettttuuuurrrrnnnniiiinnnngggg aaaa SSSSccccaaaallllaaaarrrr
-
- Now for an example of dealing with the values returned from
- a Perl sub.
-
- Here is a Perl sub, _A_d_d_e_r, which takes 2 integer parameters
- and simply returns their sum.
-
- sub Adder
- {
- my($a, $b) = @_ ;
- $a + $b ;
- }
-
- As we are now concerned with the return value from _A_d_d_e_r,
- the C function is now a bit more complex.
-
- static void
- call_Adder(a, b)
- int a ;
- int b ;
- {
- dSP ;
- int count ;
-
- ENTER ;
- SAVETMPS;
-
- PUSHMARK(sp) ;
- XPUSHs(sv_2mortal(newSViv(a)));
- XPUSHs(sv_2mortal(newSViv(b)));
- PUTBACK ;
-
- count = perl_call_pv("Adder", G_SCALAR);
-
- SPAGAIN ;
-
- if (count != 1)
- croak("Big trouble\n") ;
-
- printf ("The sum of %d and %d is %d\n", a, b, POPi) ;
-
-
-
- Page 7 (printed 6/30/95)
-
-
-
-
-
-
- PPPPEEEERRRRLLLLCCCCAAAALLLLLLLL((((1111)))) UUUUNNNNIIIIXXXX SSSSyyyysssstttteeeemmmm VVVV ((((RRRReeeelllleeeeaaaasssseeee 0000....0000 PPPPaaaattttcccchhhhlllleeeevvvveeeellll 00000000)))) PPPPEEEERRRRLLLLCCCCAAAALLLLLLLL((((1111))))
-
-
-
- PUTBACK ;
- FREETMPS ;
- LEAVE ;
- }
-
- Points to note this time are
-
- 1. The only flag specified this time was G_SCALAR. That
- means the @_ array will be created and that the value
- returned by _A_d_d_e_r will still exist after the call to
- _p_e_r_l__c_a_l_l__p_v.
-
- 2. Because we are interested in what is returned from
- _A_d_d_e_r we cannot specify G_DISCARD. This means that we
- will have to tidy up the Perl stack and dispose of any
- temporary values ourselves. This is the purpose of
-
- ENTER ;
- SAVETMPS ;
-
- at the start of the function, and
-
- FREETMPS ;
- LEAVE ;
-
- at the end. The ENTER/SAVETMPS pair creates a boundary
- for any temporaries we create. This means that the
- temporaries we get rid of will be limited to those
- which were created after these calls.
-
- The FREETMPS/LEAVE pair will get rid of any values
- returned by the Perl sub, plus it will also dump the
- mortal SV's we created. Having ENTER/SAVETMPS at the
- beginning of the code makes sure that no other mortals
- are destroyed.
-
- 3. The purpose of the macro SPAGAIN is to refresh the
- local copy of the stack pointer. This is necessary
- because it is possible that the memory allocated to the
- Perl stack has been re-allocated whilst in the
- _p_e_r_l__c_a_l_l__p_v call.
-
- If you are making use of the Perl stack pointer in your
- code you must always refresh the your local copy using
- SPAGAIN whenever you make use of of the _p_e_r_l__c_a_l_l_*
- functions or any other Perl internal function.
-
- 4. Although only a single value was expected to be
- returned from _A_d_d_e_r, it is still good practice to check
- the return code from _p_e_r_l__c_a_l_l__p_v anyway.
-
- Expecting a single value is not quite the same as
-
-
-
- Page 8 (printed 6/30/95)
-
-
-
-
-
-
- PPPPEEEERRRRLLLLCCCCAAAALLLLLLLL((((1111)))) UUUUNNNNIIIIXXXX SSSSyyyysssstttteeeemmmm VVVV ((((RRRReeeelllleeeeaaaasssseeee 0000....0000 PPPPaaaattttcccchhhhlllleeeevvvveeeellll 00000000)))) PPPPEEEERRRRLLLLCCCCAAAALLLLLLLL((((1111))))
-
-
-
- knowing that there will be one. If someone modified
- _A_d_d_e_r to return a list and we didn't check for that
- possibility and take appropriate action the Perl stack
- would end up in an inconsistant state. That is
- something you _r_e_a_l_l_y don't want to ever happen.
-
- 5. The POPi macro is used here to pop the return value
- from the stack. In this case we wanted an integer, so
- POPi was used.
-
- Here is the complete list of POP macros available,
- along with the types they return.
-
- POPs SV
- POPp pointer
- POPn double
- POPi integer
- POPl long
-
-
- 6. The final PUTBACK is used to leave the Perl stack in a
- consistant state before exiting the function. This is
- necessary because when we popped the return value from
- the stack with POPi it only updated our local copy of
- the stack pointer. Remember, PUTBACK sets the global
- stack pointer to be the same as our local copy.
-
- EEEExxxxaaaammmmpppplllleeee 4444:::: RRRReeeettttuuuurrrrnnnniiiinnnngggg aaaa lllliiiisssstttt ooooffff vvvvaaaalllluuuueeeessss
-
- Now, let's extend the previous example to return both the
- sum of the parameters and the difference.
-
- Here is the Perl sub
-
- sub AddSubtract
- {
- my($a, $b) = @_ ;
- ($a+$b, $a-$b) ;
- }
-
- and this is the C function
-
- static void
- call_AddSubtract(a, b)
- int a ;
- int b ;
- {
- dSP ;
- int count ;
-
- ENTER ;
- SAVETMPS;
-
-
-
- Page 9 (printed 6/30/95)
-
-
-
-
-
-
- PPPPEEEERRRRLLLLCCCCAAAALLLLLLLL((((1111)))) UUUUNNNNIIIIXXXX SSSSyyyysssstttteeeemmmm VVVV ((((RRRReeeelllleeeeaaaasssseeee 0000....0000 PPPPaaaattttcccchhhhlllleeeevvvveeeellll 00000000)))) PPPPEEEERRRRLLLLCCCCAAAALLLLLLLL((((1111))))
-
-
-
- PUSHMARK(sp) ;
- XPUSHs(sv_2mortal(newSViv(a)));
- XPUSHs(sv_2mortal(newSViv(b)));
- PUTBACK ;
-
- count = perl_call_pv("AddSubtract", G_ARRAY);
-
- SPAGAIN ;
-
- if (count != 2)
- croak("Big trouble\n") ;
-
- printf ("%d - %d = %d\n", a, b, POPi) ;
- printf ("%d + %d = %d\n", a, b, POPi) ;
-
- PUTBACK ;
- FREETMPS ;
- LEAVE ;
- }
-
- Notes
-
- 1. We wanted array context, so we used G_ARRAY.
-
- 2. Not surprisingly there are 2 POPi's this time because
- we were retrieving 2 values from the stack. The main
- point to note is that they came off the stack in
- reverse order.
-
- EEEExxxxaaaammmmpppplllleeee 5555:::: RRRReeeettttuuuurrrrnnnniiiinnnngggg DDDDaaaattttaaaa ffffrrrroooommmm PPPPeeeerrrrllll vvvviiiiaaaa tttthhhheeee ppppaaaarrrraaaammmmeeeetttteeeerrrr lllliiiisssstttt
-
- It is also possible to return values directly via the
- parameter list - whether it is actually desirable to do it
- is another matter entirely.
-
- The Perl sub, _I_n_c, below takes 2 parameters and increments
- each.
-
- sub Inc
- {
- ++ $_[0] ;
- ++ $_[1] ;
- }
-
- and here is a C function to call it.
-
-
-
-
-
-
-
-
-
-
- Page 10 (printed 6/30/95)
-
-
-
-
-
-
- PPPPEEEERRRRLLLLCCCCAAAALLLLLLLL((((1111)))) UUUUNNNNIIIIXXXX SSSSyyyysssstttteeeemmmm VVVV ((((RRRReeeelllleeeeaaaasssseeee 0000....0000 PPPPaaaattttcccchhhhlllleeeevvvveeeellll 00000000)))) PPPPEEEERRRRLLLLCCCCAAAALLLLLLLL((((1111))))
-
-
-
- static void
- call_Inc(a, b)
- int a ;
- int b ;
- {
- dSP ;
- int count ;
- SV * sva ;
- SV * svb ;
-
- ENTER ;
- SAVETMPS;
-
- sva = sv_2mortal(newSViv(a)) ;
- svb = sv_2mortal(newSViv(b)) ;
-
- PUSHMARK(sp) ;
- XPUSHs(sva);
- XPUSHs(svb);
- PUTBACK ;
-
- count = perl_call_pv("Inc", G_DISCARD);
-
- if (count != 0)
- croak ("call_Inc : expected 0 return value from 'Inc', got %d\n", count) ;
-
- printf ("%d + 1 = %d\n", a, SvIV(sva)) ;
- printf ("%d + 1 = %d\n", b, SvIV(svb)) ;
-
- FREETMPS ;
- LEAVE ;
- }
-
- To be able to access the two parameters that were pushed
- onto the stack after they return from _p_e_r_l__c_a_l_l__p_v it is
- necessary to make a note of their addresses - thus the two
- variables sva and svb.
-
- The reason this is necessary is that the area of the Perl
- stack which held them will very likely have been overwritten
- by something else by the time control returns from
- _p_e_r_l__c_a_l_l__p_v.
-
- EEEExxxxaaaammmmpppplllleeee 6666:::: UUUUssssiiiinnnngggg GGGG____EEEEVVVVAAAALLLL
-
- Now an example using G_EVAL. Below is a Perl sub which
- computes the difference of its 2 parameters. If this would
- result in a negative result, the sub calls _d_i_e.
-
- sub Subtract
- {
- my ($a, $b) = @_ ;
-
-
-
- Page 11 (printed 6/30/95)
-
-
-
-
-
-
- PPPPEEEERRRRLLLLCCCCAAAALLLLLLLL((((1111)))) UUUUNNNNIIIIXXXX SSSSyyyysssstttteeeemmmm VVVV ((((RRRReeeelllleeeeaaaasssseeee 0000....0000 PPPPaaaattttcccchhhhlllleeeevvvveeeellll 00000000)))) PPPPEEEERRRRLLLLCCCCAAAALLLLLLLL((((1111))))
-
-
-
- die "death can be fatal\n" if $a < $b ;
-
- $a - $b ;
- }
-
- and some C to call it
-
- static void
- call_Subtract(a, b)
- int a ;
- int b ;
- {
- dSP ;
- int count ;
- SV * sv ;
-
- ENTER ;
- SAVETMPS;
-
- PUSHMARK(sp) ;
- XPUSHs(sv_2mortal(newSViv(a)));
- XPUSHs(sv_2mortal(newSViv(b)));
- PUTBACK ;
-
- count = perl_call_pv("Subtract", G_EVAL|G_SCALAR);
-
- /* Check the eval first */
- sv = GvSV(gv_fetchpv("@", TRUE, SVt_PV));
- if (SvTRUE(sv))
- printf ("Uh oh - %s\n", SvPV(sv, na)) ;
-
- SPAGAIN ;
-
- if (count != 1)
- croak ("call_Subtract : expected 1 return value from 'Subtract', got %d\n", count) ;
-
- printf ("%d - %d = %d\n", a, b, POPi) ;
-
- PUTBACK ;
- FREETMPS ;
- LEAVE ;
-
- }
-
- If _c_a_l_l__S_u_b_t_r_a_c_t is called thus
-
- call_Subtract(4, 5)
-
- the following will be printed
-
- Uh oh - death can be fatal
-
-
-
-
- Page 12 (printed 6/30/95)
-
-
-
-
-
-
- PPPPEEEERRRRLLLLCCCCAAAALLLLLLLL((((1111)))) UUUUNNNNIIIIXXXX SSSSyyyysssstttteeeemmmm VVVV ((((RRRReeeelllleeeeaaaasssseeee 0000....0000 PPPPaaaattttcccchhhhlllleeeevvvveeeellll 00000000)))) PPPPEEEERRRRLLLLCCCCAAAALLLLLLLL((((1111))))
-
-
-
- Notes
-
- 1. We want to be able to catch the _d_i_e so we have used the
- G_EVAL flag. Not specifying this flag would mean that
- the program would terminate.
-
- 2. The code
-
- sv = GvSV(gv_fetchpv("@", TRUE, SVt_PV));
- if (SvTRUE(sv))
- printf ("Uh oh - %s\n", SvPVx(sv, na)) ;
-
- is the equivalent of this bit of Perl
-
- print "Uh oh - $@\n" if $@ ;
-
-
- EEEExxxxaaaammmmpppplllleeee 7777:::: UUUUssssiiiinnnngggg ppppeeeerrrrllll____ccccaaaallllllll____ssssvvvv
-
- In all the previous examples I have 'hard-wried' the name of
- the Perl sub to be called from C. Sometimes though, it is
- necessary to be able to specify the name of the Perl sub
- from within the Perl script.
-
- Consider the Perl code below
-
- sub fred
- {
- print "Hello there\n" ;
- }
-
- CallSub("fred") ;
-
- here is a snippet of XSUB which defines _C_a_l_l_S_u_b.
-
- void
- CallSub(name)
- char * name
- CODE:
- PUSHMARK(sp) ;
- perl_call_pv(name, G_DISCARD|G_NOARGS) ;
-
- That is fine as far as it goes. The thing is, it only allows
- the Perl sub to be specified as a string. For perl 4 this
- was adequate, but Perl 5 allows references to subs and
- anonymous subs. This is where _p_e_r_l__c_a_l_l__s_v is useful.
-
- The code below for _C_a_l_l_S_u_b is identical to the previous time
- except that the name parameter is now defined as an SV* and
- we use _p_e_r_l__c_a_l_l__s_v instead of _p_e_r_l__c_a_l_l__p_v.
-
-
-
-
-
- Page 13 (printed 6/30/95)
-
-
-
-
-
-
- PPPPEEEERRRRLLLLCCCCAAAALLLLLLLL((((1111)))) UUUUNNNNIIIIXXXX SSSSyyyysssstttteeeemmmm VVVV ((((RRRReeeelllleeeeaaaasssseeee 0000....0000 PPPPaaaattttcccchhhhlllleeeevvvveeeellll 00000000)))) PPPPEEEERRRRLLLLCCCCAAAALLLLLLLL((((1111))))
-
-
-
- void
- CallSub(name)
- SV* name
- CODE:
- PUSHMARK(sp) ;
- perl_call_sv(name, G_DISCARD|G_NOARGS) ;
-
- As we are using an SV to call _f_r_e_d the following can all be
- used
-
- CallSub("fred") ;
- Callsub(\&fred) ;
- $ref = \&fred ;
- CallSub($ref) ;
- CallSub( sub { print "Hello there\n" } ) ;
-
- As you can see, _p_e_r_l__c_a_l_l__s_v gives you greater flexibility
- in how you can specify the Perl sub.
-
- EEEExxxxaaaammmmpppplllleeee 8888:::: UUUUssssiiiinnnngggg ppppeeeerrrrllll____ccccaaaallllllll____aaaarrrrggggvvvv
-
- Here is a Perl sub which prints whatever parameters are
- passed to it.
-
- sub PrintList
- {
- my(@list) = @_ ;
-
- foreach (@list) { print "$_\n" }
- }
-
- and here is an example of _p_e_r_l__c_a_l_l__a_r_g_v which will call
- _P_r_i_n_t_L_i_s_t.
-
- call_PrintList
- {
- dSP ;
- char * words[] = {"alpha", "beta", "gamma", "delta", NULL } ;
-
- perl_call_argv("PrintList", words, G_DISCARD) ;
- }
-
- Note that it is not necessary to call PUSHMARK in this
- instance. This is because _p_e_r_l__c_a_l_l__a_r_g_v will do it for you.
-
- EEEExxxxaaaammmmpppplllleeee 9999:::: UUUUssssiiiinnnngggg ppppeeeerrrrllll____ccccaaaallllllll____mmmmeeeetttthhhhoooodddd
-
- [This section is under construction]
-
- Consider the following Perl code
-
-
-
-
-
- Page 14 (printed 6/30/95)
-
-
-
-
-
-
- PPPPEEEERRRRLLLLCCCCAAAALLLLLLLL((((1111)))) UUUUNNNNIIIIXXXX SSSSyyyysssstttteeeemmmm VVVV ((((RRRReeeelllleeeeaaaasssseeee 0000....0000 PPPPaaaattttcccchhhhlllleeeevvvveeeellll 00000000)))) PPPPEEEERRRRLLLLCCCCAAAALLLLLLLL((((1111))))
-
-
-
- {
- package Mine ;
-
- sub new { bless [@_] }
- sub Display { print $_[0][1], "\n" }
- }
-
- $a = new Mine ('red', 'green', 'blue') ;
- call_Display($a, 'Display') ;
-
- The method Display just prints out the first element of the
- list. Here is a XSUB implementation of _c_a_l_l__D_i_s_p_l_a_y.
-
- void
- call_Display(ref, method)
- SV * ref
- char * method
- CODE:
- PUSHMARK(sp);
- XPUSHs(ref);
- PUTBACK;
-
- perl_call_method(method, G_DISCARD) ;
-
-
- SSSSttttrrrraaaatttteeeeggggiiiieeeessss ffffoooorrrr ssssttttoooorrrriiiinnnngggg CCCCoooonnnntttteeeexxxxtttt IIIInnnnffffoooorrrrmmmmaaaattttiiiioooonnnn
-
- [This section is under construction]
-
- One of the trickiest problems to overcome when designing a
- callback interface is figuring out how to store the mapping
- between the C callback functions and the Perl equivalent.
-
- Consider the following example.
-
- AAAAlllltttteeeerrrrnnnnaaaatttteeee SSSSttttaaaacccckkkk MMMMaaaannnniiiippppuuuullllaaaattttiiiioooonnnn
-
- [This section is under construction]
-
- Although I have only made use of the POP* macros to access
- values returned from Perl subs, it is also possible to
- bypass these macros and read the stack directly.
-
- The code below is example 4 recoded to
-
- SSSSEEEEEEEE AAAALLLLSSSSOOOO
- the _p_e_r_l_a_p_i manpage, the _p_e_r_l_g_u_t_s manpage, the _p_e_r_l_e_m_b_e_d
- manpage
-
- AAAAUUUUTTTTHHHHOOOORRRR
- Paul Marquess <pmarquess@bfsec.bt.co.uk>
-
-
-
-
- Page 15 (printed 6/30/95)
-
-
-
-
-
-
- PPPPEEEERRRRLLLLCCCCAAAALLLLLLLL((((1111)))) UUUUNNNNIIIIXXXX SSSSyyyysssstttteeeemmmm VVVV ((((RRRReeeelllleeeeaaaasssseeee 0000....0000 PPPPaaaattttcccchhhhlllleeeevvvveeeellll 00000000)))) PPPPEEEERRRRLLLLCCCCAAAALLLLLLLL((((1111))))
-
-
-
- Special thanks to the following people who assisted in the
- creation of the document.
-
- Jeff Okamoto, Tim Bunce.
-
- DDDDAAAATTTTEEEE
- Version 0.4, 17th October 1994
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- Page 16 (printed 6/30/95)
-
-
-
-