Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Hi, Can any one please tell me how to pass the reference to perl script from C code. The problem is, we have created perl interpreter instance and calling the perl subroutine using call_pv* interface. We are having a string consists of huge (in terms of MB) data. Shall we pass the address (char *) of the string to perl using XPUSH() and access the string from perl subroutine. It would be helpful if you can suggest something in this regard because we have huge data (in terms of MB) in C code and that should be processed by perl function. If we send the entire data, every time, we think we will be running out memory. Thanks, poorna

Replies are listed 'Best First'.
(tye)Re: Passing reference to perl from C code
by tye (Sage) on Dec 21, 2000 at 21:41 UTC

    For most things that Perl does with strings, it will want the string in a scalar, which implies that the string is in a buffer that Perl allocated. So most ways that you turn a C char * into a Perl string involve allocating a new buffer (often using strlen() to determine the size) and then copying the entire string into the new buffer.

    The problem with this usually isn't running out of memory (since the space is usually reused if you do this over and over), but that all of the copying gets slow.

    My personal preference when dealing with large strings going between Perl and C is to have Perl allocate a big scalar and put the string in there and then have C deal with that copy of the string. This is because Perl nearly refuses to even look at strings if they aren't in buffers that it allocated. [ About the only thing Perl can do with other strings involves using several iterations of pack() and unpack() to copy parts of the string into Perl's own buffers or just passing the pointer around to other C code. ]

    Usually the C APIs don't have much problem with this. Rarely you find C code that insists on allocating its own buffers and also refuses to deal properly with a moved copy of the data.

    Exactly how to allocate this SV to hold your string depends on, well, the details, which you didn't give. Feel free to post details if you have problems there.

            - tye (but my friends call me "Tye")
      Hi Tye, Yes, You are correct. Thanks. We have to copy the string (using XPUSH) into perl stack. Our problem will be like this, we have to call the perl subroutine with some 200 to 300 string of huge size during every invocation. Please see the below C code used to call the perl subroutine.
      please note that the macros are not in the order. dSP; SAVETEMPS; PUSKMARK; XPUSHs(SV equivalent for the First string); XPUSHs(SV equivalent for the second string); : : : XPUSHs(SV equivalent for the Nth string); call_pv("pkg1::Process",G_SCALAR); something goes here
      If we have something like passing the address of the strings as in the C calls
      void Process(char *p) { // some processing } main() { char Str[500]; strcpy(Str,"slkdjaslkdjsljdlkjas"); Process(&Str);
      Is there any way to push the address of the string using XPUSHs() and that string can be used by the perl script?. Regards, Poorna

        Is there any way to push the address of the string using XPUSHs() and that string can be used by the perl script?

        I suggest you reread my previous reply as you don't appear to have understood it. The answer is "no". The best you can do is not recreate the SV equivalent of each string over and over.

                - tye (but my friends call me "Tye")
Re: Passing reference to perl from C code
by Dominus (Parson) on Dec 21, 2000 at 20:23 UTC
    You said you might pass a cahr *, or you might do something else, and you are afraid of running out of memory if you do something else. But you didn't say what something else might be.

    In C, there is no way to pass a string by value. The only way to pass a string is by passing a pointer to its first character. So I do not know what you are worried about or what you think the problem will be.

Re: Passing reference to perl from C code
by c-era (Curate) on Dec 21, 2000 at 19:41 UTC
    If I remeber what I've read correctly (and if I haven't someone will let me know), if you are just calling a subroutine the memory should be released when the subroutine is finished. If it isn't you can use the G_EVAL flag which will make sure the memory is released.

    If perl does keep the memory contents, when you pass your pointer it should overwrite the memory contents with your new info, or take advantage of the fact and just send the info that needs updating (you will need to change the perl script to accept this).

    I do not have the time to write a test script, but if you do try these suggestions I would be interested in finding out how it works.