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

I am trying to do an interpreter between perl and C (win32). The proplem is that none of the examples found at the www, perldoc and etc do not show how to pass a typedef structures from C to perl, manipulate the data, and then return the data back to the C. For example If defined structure in C is
typedef struct { int Msg; short uid; short LineType; char *Line; // pointer to text char Sound; // 0 for no sound, otherwise sound # char *Cmd; // String of command including ! char OpOnly; // Is the command for ops only? char *HelpFile; // Help Filename for this command int CmdID; // CmdID - The bot core doesn't care about this +, } BOTMSG;
I would love to see this stucture in perl like a HASH or OO. For example
sub set_struct { xxxxx = @_; # <-- ??? $BOTMSG{Msg} = "1"; $BOTMSG{uid} = "1024"; $BOTMSG{LineType} = "100"; $BOTMSG{Line} = "Hello World\n"; $BOTMSG{Cmd} = "/!say"; $BOTMSG{OpOnly} = "yes"; $BOTMSG{HelpFile} = "C\tmp\help\help1.txt"; $BOTMSG{CmdID} = "1234567890"; return %BOTMSG; };
The given examples in Perlembed documentation only show how to pass a single pointer, an integer, or an string value but it do not say anything about passing complex structures and how to manipulate those in perl. I appreciate all help you can give me,
-jari-
jari@subspace.nu

Replies are listed 'Best First'.
Re: Empedding perl into C - how to pass complex structures to the perl ?
by broquaint (Abbot) on Feb 27, 2002 at 13:16 UTC
    Check out Inline::Struct for all your C/C++ struct munging needs :-)
    HTH

    broquaint

Re: Empedding perl into C - how to pass complex structures to the perl ?
by Matts (Deacon) on Feb 27, 2002 at 13:16 UTC
    If you want it to be a hash you're going to have to construct a HV*, and possibly create a reference to it (RV) to pass to Perl. None of this is automatic for you, and if you do construct a hash don't expect the values to be updatable automatically. See perlguts for details on doing all of that.

    Alternatively hide it in a class, and provide accessors to the data parts. See the XS Cookbook (CookbookA and CookbookB on CPAN) for details on how to do this.

      Thnx you, I am seeing that this will not going to be ez for me : ). Any possibilities you could write a small example by constructing a HV*, creating the reference and then passing it to perl, please, please ?=) ..Im sure this is the solution I am looking out. -jari- jari@subspace.nu