Hi Rob Thanks again for an immediate reply in between your schedule of work. I was hoping for a neat solution using the Inline::Struct.
I need a solution for this problem. I have a nested structure as given in the example below. This is a 3-level nested structure and a linked list. I have a parser module that parses some files and I need to populate the structures with these values. I would like use these values within the structure for further processing in C program. I am not sure how should I do the same using only Inline::C . How should I pass the structure reference between C and Perl?? If you can give me suggestions that would be great. I have actually seen the Object oriented perl and modified it according to my requirement. It doesn't work. To give an idea about what code I wrote, here it is: ------------------------------------------------------------example.pl-- --------------------------- my $obj = str1->new(); print "object number is $obj->num \n"; print "object string is $obj->str\n"; print "object is $obj\n"; $obj->set_num(10); print "The string from perl *** ", $obj->set_str("hello"), " ** ENg\n"; #print "The name is --> ", $obj->get_n, "\n"; $obj->myfunc("something"); #------------------------------ package str1; use Inline C => <<'END'; typedef struct{ char cmd[10]; char output[10]; }cmd_t; typedef struct{ int num; char* str; cmd_t val; }str1; str1* s1; SV* new(char* class) { s1 = malloc(sizeof(str1)); SV* obj_ref= newSViv(0); SV* obj = newSVrv(obj_ref,class); sv_setiv(obj,(IV)s1); return obj_ref; } void set_num(char* class, int obj) { ((str1*)class)->num = obj; printf("%i\n",((str1*)class)->num); printf("'%i'\n",&((str1*)class)->num); } char* set_str(char* class, char* obj) { ((str1*)class)->str = strdup(obj); ((str1*)class)->val.cmd = strdup(obj); ((str1*)class)->val.output = strdup(obj); printf("'%i'\n",&((str1*)class)->str); printf("'%s'\n",((str1*)class)->str); printf("cmd '%s'\n",((str1*)class)->val->cmd); printf("output '%s'\n",((str1*)class)->val->output); return ((str1*)class)->str; } SV* get_n(char* class) { return newSVpvf(((str1*)class)->str); } void myfunc(char* class, char* xxx) { printf("%i\n",((str1*)class)->num); printf("'%i'\n",&((str1*)class)->num); printf("'%i'\n",&((str1*)class)->str); printf ("%s\n", class); printf("'%s'\n",xxx); printf("'%s'\n",&((str1*)class)->str); ((str1*)class)->str = strdup(xxx); printf("****'%s'\n",xxx); printf("'%s'\n",((str1*)class)->str); } END ------------------------------------------------------------------------ --------------------------- Thanks & Regards Sridevi -----Original Message----- From: Sisyphus [mailto:[EMAIL PROTECTED] Sent: Friday, October 13, 2006 2:08 PM To: Tallapragada, Sridevi; inline@perl.org Subject: Re: How to use Inline::Structs ----- Original Message ----- From: "Tallapragada, Sridevi" <[EMAIL PROTECTED]> To: "Sisyphus" <[EMAIL PROTECTED]>; <inline@perl.org> Sent: Friday, October 13, 2006 5:12 PM Subject: RE: How to use Inline::Structs > Hi Rob > > Thanks once again. I really appreciate your timely replies. > I did add the typedef but still got the error. > > Now I am not getting the error as per your code change suggestions. I > did take the same code as mentioned in your mail and it works. > > Here is another problem that I am facing. May be you can help me > I have modified the code to include nested structures and it doesn't > seem to work. Can you help?? > ------------------------------st.pl------------------------------------ > use Inline C => <<'EOC', STRUCTS => 1; > typedef struct myst myst; > struct myst{ > int a; > }; > typedef struct Foo Foo; > struct Foo { > int num; > char *str; > myst myst[2]; > Foo *next; > }; > > > void myfunc(Foo *f) { > printf("myfunc: num=%i, str='%s num1 = '%i' '\n", f->num, f->str, > f->myst[0].a); > } > > EOC > > my $obj = Inline::Struct::Foo->new; > $obj->num(10); > $obj->str("Hello"); > $obj->myst[0]->a(10); > > myfunc($obj); > ------------------------------------------------------------------ > > I get the following error > > perl st.pl > /usr/bin/perl /usr/lib/perl5/5.8.5/ExtUtils/xsubpp -typemap > /usr/lib/perl5/5.8.5/ExtUtils/typemap -typemap > /usr/lib/perl5/5.8.5/ExtUtils/typemap -typemap > /root/sridevi/_Inline/build/st_pl_d994/Struct.map st_pl_d994.xs > > st_pl_d994.xsc && mv st_pl_d994.xsc st_pl_d994.c > Error: 'Foo *' not in typemap in st_pl_d994.xs, line 220 > make: *** [st_pl_d994.c] Error 1 > > A problem was encountered while attempting to compile and install your > Inline > C code. The command that failed was: > make > out.make 2>&1 > > The build directory was: > /root/sridevi/_Inline/build/st_pl_d994 > > To debug the problem, cd to the build directory, and inspect the output > files. > > at st.pl line 1 > BEGIN failed--compilation aborted at st.pl line 19. > > ------------------------------------------------------------------------ > --------- > > When I opened the Struct.map file > In the typemap section, I am able to see only ,struct myst and not > struct Foo... > "TYPEMAP > struct myst * O_OBJECT_myst" > > Why is that??? > This module is apparently very buggy - perhaps to the point of unusability. It hasn't been updated since 2003 - in fact I don't think Neil has updated *any* of his modules in a long time. I think your best bet is to to do what you want with Inline::C and forget about the neat tricks that Inline::Struct is supposed to do for you. As a further example of the bugginess of this module, this is what I get if I replace 'myst myst[2];' with '/* myst myst[2]; */': D:\pscrpt\inline\struct>perl demo.pl Can't use string ("/* myst myst[2]; */") as an ARRAY ref while "strict refs" in use at (eval 49) line 591. BEGIN failed--compilation aborted at demo.pl line 20. That happens before Inline::Struct even tries to compile the C code. Apparently there's a problem separating the C code out from the perl code. (I didn't realise it was *that* buggy.) Perhaps these are bugs that can easily be fixed (though I have my doubts). I don't have time to look right now. I'll probably look a little more closely at it when I do get time .... in a day or 2 (or 3 :-) Like I said, I think, given the apparent degree of bugginess of Inline::Struct, that the best thing is to just use Inline::C. It will handle structs fine. Cheers, Rob