in reply to Extending & Embedding Perl simultaneously: How to share a scalar between perl callback sub and main body?

Works for me (ActiveState Perl 10.1 on Windoze).

Did you miss the syntax error in your code (missing ; after until)? Try use warnings;

Here is my C:
#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
Here is the Perl (its a long time since I didn't use strict;):
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";
and here is the output:
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

Update: original code was not passing parameters to the callback.
Update 2: I hadn't freed the temporaries and closed the scope
  • Comment on Re: Extending & Embedding Perl simultaneously: How to share a scalar between perl callback sub and main body?
  • Select or Download Code

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

    My guess would be the problem is that the OP is using perl_clone() (which you are not), and that the callback code is being executed in the wrong (cloned) interpreter.   (As usual, it would help to see the OP's actual code, though...)

      Thanks for all the comments!

      Apologies for typo in the code sample. I tried prefixing "our", but that didnt change the result.

      As almut said, I am using a cloned interpreter and the perl sub/callback is executed in the cloned interpreter context. So, is the problem in how I'm cloning it? Or does cloning by definition mean, I cannot "share". Here is a post describing how I've done the embedding.

      One "crude" way I can think of is to implement a "shared memory" API on C side to save/retrieve data between the subs and main. Perhaps CPAN has modules like this already..but I'm really hoping there is a more "natural" solution.

      http://markmail.org/thread/cjbhybjfikuirvud

      http://markmail.org/thread/inetppjegt5iecl5

        Hi All,

        Hoping to get some pointers as I am really stuck here. I couldnt yet figure out the cause of the problem.

        I also couldnt find any package in CPAN to "hack" around this issue : most sharing related packages seem really geared toward multi-process, distributed sharing or persistance. None that are simple/for my case. I guess I've to write my own workaround? :(

        Thx

        ps: I'm in Asian timezone, hence the "delays" in responding back to posts.