----- PERL USENET appended at 03:42:54 on 92/01/14 GMT (by USENET at ALMADEN) - Subject: Re: Callback in perl From: lwall@netlabs.com (Larry Wall) Message-ID: <1992Jan14.030141.28337@netlabs.com> Date: 14 Jan 92 03:01:41 GMT Article-I.D.: netlabs.1992Jan14.030141.28337 References: <9201081932.AA17121@ucbvax.Berkeley.EDU> Organization: NetLabs, Inc. In article <9201081932.AA17121@ucbvax.Berkeley.EDU> TEDLAW@TOROLAB6.VNET.IBM.COM ("Theodore C. Law") writes: : I'm trying to add an X11/Motif binding to perl using curseperl in perl/usub : as an example. Each Xt or Xm routine will be defined to perl as a perl- : callable subroutine. Typically, a perl script would create its widget : hierarchy by calling the Xm/Xt perl subroutines, and then call &XtMainLoop. : I would also like to be able to register perl callback routines. : The first idea that comes to mind is to wrap the perl subroutine name, : the perl client data (a STR), into a callback record whose pointer : is then passed to XtAddCallback. The C routine that actually gets called back : from X is perl_callback_dispatcher(). It will unwrap the callback record : pointer to obtain the perl subroutine name, etc; build the argument : list on the stack, and invoke the perl subroutine. : : My question is what should I pass as "sp" to "callback()" in usersub.c? Whatever the value of sp was when you called into C (presumably when you called XtMainLoop), plus however many arguments you pushed on the stack (see callv() for an example of that). If you want to go back and forth between Perl and C recursively you'll have to save and restore the "previous" value of sp as you enter and exit C each time. Larry ÿÿRe: Callback in perl I N §MSG From: ARCNET --ALMVMA To: TEDLAW --TOROLAB6 01/16/92 21:45:37á ========================================================================= ----- PERL USENET appended at 02:44:03 on 92/01/17 GMT (by USENET at ALMADEN) - Subject: Re: Passing an array to a C glue routine From: lwall@netlabs.com (Larry Wall) Message-ID: <1992Jan17.015922.17263@netlabs.com> Date: 17 Jan 92 01:59:22 GMT References: Distribution: comp Organization: NetLabs, Inc. In article neeri@iis.ethz.ch (Matthias Ulrich Neeracher) writes: : I have written a C subroutine for my usersub.c that I would like to take an : array paramter (input only). Unfortunately, from the description in : usub/README, which only explains how to return an array, I haven't been able to : figure it out. From the point of view of a usersub routine, it can't tell the difference between &foo(1,2,3); and @args = (1,2,3); &foo(@args); because all arguments are reduced to a one-dimensional LIST. So you treat array values just like a list of scalar values, and they'd come in as st[1], st[2], etc. If you want to pass a *foo value, that just comes through as a scalar. So you could call it like this: &foo(*args); and then in your glue routines, you'd need something like ARRAY *ar = stab_array(st[1]); to pull out the array pointer from the symbol table entry. Then ar->ary_fill tells you the subscript of the last element, and you can use afetch() and astore() to get and set values in the array. Larry ----- PERL USENET appended at 11:33:12 on 92/01/09 GMT (by USENET at ALMADEN) - Subject: Re: Callback in perl From: steven@dev.state.COM.AU (Steven Sweeting) Message-ID: Date: 9 Jan 92 09:04:46 GMT References: <9201081932.AA17121@ucbvax.Berkeley.EDU> Sender: news@state.COM.AU Organization: Group Treasury, State Bank of NSW TEDLAW@TOROLAB6.VNET.IBM.COM ("Theodore C. Law") writes: >I'm trying to add an X11/Motif binding to perl using curseperl in perl/usub >as an example. Each Xt or Xm routine will be defined to perl as a perl- >callable subroutine. Typically, a perl script would create its widget >hierarchy by calling the Xm/Xt perl subroutines, and then call &XtMainLoop. >I would also like to be able to register perl callback routines. >My question is what should I pass as "sp" to "callback()" in usersub.c? I haven't worked out how to call perl multiple times from C, so my program calls perl's main, which then calls my application main which can then call perl functions as you are after. This being the case, any "magic" function that is called from perl (for example "addch" in curses.mus) will be done through a C handler. In this case through :- static int usersub(ix, sp, items) int ix; register int sp; register int items; () Note the second argument! I push this onto my own stack, (because we can nest perl->C->perl->C->perl) and my callback function looks something like :- /**************************************************************** * C++ versions of callv - cast return result to int */ void blot::Callv(const char* funcname, int& retval) { int orig_sp = blot::top_sp(); // What was the last sp? int new_sp = ::callv(funcname, orig_sp, 0, NULL); STR **st = ::stack->ary_array + orig_sp; retval = (int)str_gnum(st[1]); // Read variable off stack printf("called callv(funcname=&%s, st[1]=\"%s\", ret_sp=%d)=%lf\n", funcname, str_get(st[1]), new_sp, retval); while (tmps_max > tmps_base) // Clear temp variables on stack (if any) { str_free(tmps_list[tmps_max]); tmps_list[tmps_max--] = NIL(STR*); } // Don't alter sp, as we have cleared then out anyway // ie really doing a blot::set_top_sp(orig_sp); } Only blot::top_sp() and blot::set_top_sp(orig_sp) are my functions, others are perl variables/functions. WARNING: I have just begun to play with this, and today is the first time it works without a memory leak. Since I don't plan on returning back to the code that called my "&main()", then I need to clean up temporary variables; these are on tmps_list[]. I presume I am effectively cleaning up and temporary variables that may be on the stack. Once I do return to perl from my "usersub", I fetch the last "sp" from my stack, which was the last sp returned to me from perl. Normally NOT THE ORIGINAL sp, but the LAST one, unless as I HAVE DONE ABOVE, and ignore anything that has been added to the stack. Good luck, Steven -- -----------------8<-------------------8<-------------------8<---------------- Steven Sweeting, Treasury Systems, steven@dev.state.com.au State Bank of New South Wales, Grosvenor Place ph. +61 2 259-4144 L40 225 George St, Sydney, NSW 2000, Australia fax. +61 2 251-8009