Here is the Perl (its a long time since I didn't use strict;):#include "EXTERN.h" #include "perl.h" #include "XSUB.h" #include "ppport.h" #include "const-c.inc" MODULE = MyModule PACKAGE = MyModule INCLUDE: const-xs.inc int RegisterCB (SV *SubRef) CODE: ENTER; SAVETMPS; PUSHMARK(SP); XPUSHs(sv_2mortal(newSVpv("Some event",0))); XPUSHs(sv_2mortal(newSVpv("Some pdata",0))); PUTBACK; call_sv(SubRef, G_DISCARD); FREETMPS; LEAVE; RETVAL = 1; OUTPUT: RETVAL
and here is the output:use MyModule; use warnings; #shared scalar $cb_done = 0; #This cb would be invoked by the cloned interpreter in the #C library. This works fine. sub cb_one { ($event, $pdata) = @_; #This line is printed. print "event : ", $event, "\n"; $cb_done = 1; } $status = MyModule::RegisterCB(\&main::cb_one); do { sleep (5); } until ($cb_done == 1); #This line is printed as $cb_done becomes 1 #in the main of the perl script print "CB was invoked : $cb_done\n";
C:\gash\MyModule>test.pl Name "main::pdata" used only once: possible typo at C:\gash\MyModule\t +est.pl line 11. Name "main::status" used only once: possible typo at C:\gash\MyModule\ +test.pl line 17. event : Some event CB was invoked : 1
In reply to Re: Extending & Embedding Perl simultaneously: How to share a scalar between perl callback sub and main body?
by cdarke
in thread Extending & Embedding Perl simultaneously: How to share a scalar between perl callback sub and main body?
by perlmonk1729
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |