Hi,

I'm working on an application written in C++ which incorporates Apache's
Thrift, PHP, Perl and some other tools.

My problem is that when I call C++ function from Perl (as XS) and pass the
arguments by reference I cannot change their values in the C++ code
so that the changes will persist in the Perl code too.

I would like to illustrate my problem with the following Perl and C++
snippets and the results I get.

*This is the example Perl code I use:*
---- SNIP SNIP ----
sub process {
    my ($self, $input, $output) = @_;
    my $weight = 80;

    print "[PERL] before read() : weight = $weight \n";

    $input->read(\$weight);

    print "[PERL] after read() : weight = $weight \n";

    return 1;
}
---- SNIP SNIP ----


*This is the C++ code:*
---- SNIP SNIP ----
XS(TMultiplexedProtocolRead_read) {
    dXSARGS;
    {
        cout << "[C++] what was received: " << endl;
        sv_dump(ST(1));

        sv_setref_pv(ST(1), NULL, (void *) 4545);
        cout << "[C++] after the new value was set: " << endl;
        sv_dump(ST(1));
    }
};
---- SNIP SNIP ----

*In some point in the C++ code I have the following:*
---- SNIP SNIP ----
    dSP;
    ENTER;
    SAVETMPS;
    PUSHMARK(sp);

    XPUSHs(ref_to_obj_protocol_read);
    PUTBACK;

    call_method("Dispatch::init", G_DISCARD);

    FREETMPS;
    LEAVE;
---- SNIP SNIP ----

and the Dispatch::init() will call the process() (the code of the process()
you can find in the beginning of this message) .

*What I get after the whole execution is this:*

[PERL] before read() : weight = 80

[C++] what was received:
SV = IV(0x2c21d50) at 0x2c21d58
  REFCNT = 1
  FLAGS = (TEMP,ROK)
  RV = 0x2c46318
    SV = PVIV(0x2850b60) at 0x2c46318
      REFCNT = 2
      FLAGS = (PADMY,IOK,POK,pIOK,pPOK)
      IV = 80
      PV = 0x2c19a80 "80"\0
      CUR = 2
      LEN = 8

[C++] after the new value was set:
SV = IV(0x2c21d50) at 0x2c21d58
  REFCNT = 1
  FLAGS = (TEMP,ROK)
  RV = 0x2c17d18
    SV = IV(0x2c17d10) at 0x2c17d18
      REFCNT = 1
      FLAGS = (IOK,pIOK)
      IV = 4545

[PERL] after read() : weight = 80

What should I do in order to change the $weight's value so that it will
persist in the Perl code after the $input->read(\$weight); is executed ?

Thank you in advance,
Have a nice day!

Reply via email to