in reply to Extending & Embedding Perl simultaneously: How to share a scalar between perl callback sub and main body?
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
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Extending & Embedding Perl simultaneously: How to share a scalar between perl callback sub and main body?
by almut (Canon) on Dec 01, 2009 at 14:26 UTC | |
by perlmonk1729 (Acolyte) on Dec 02, 2009 at 05:52 UTC | |
by perlmonk1729 (Acolyte) on Dec 03, 2009 at 08:47 UTC | |
by BrowserUk (Patriarch) on Dec 03, 2009 at 09:18 UTC | |
by perlmonk1729 (Acolyte) on Dec 03, 2009 at 16:47 UTC | |
|