For Programmers: Free Programming Magazines  


Home > Archive > PERL Modules > August 2005 > writing modules in 'c'









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author writing modules in 'c'
Narendran Kumaraguru Nathan

2005-08-27, 7:55 am

Hi All,
I've been trying to write modules in 'c'. I've gone through the
documentation of perlguts, perlapi & perlxs. Although what I'm trying to
do is much complicated to explain here, I am extracting out the problem
with a small example.
o I want to create an object (malloc'ed) and store the object's address
as SV.
o Once created & blessed, I should be able to call the function of the
object by simply telling "$object->method(params)".
I don't know what mistake I'm doing but I'm getting segmentation fault.
Sometimes I get a message saying "Attempt to change a constant's value"
or something like that. To replicate the problem do the following

1. h2xs -A -n MyPkg
2. cd MyPkg
3. Append the following code to MyPkg.xs file
SV *
new(const char *str)
PREINIT:
char* myname;
SV* svt1;
SV* svt2 = &PL_sv_undef;
HV* pkg;
CODE:
pkg = gv_stashpv("MyPkg", 0);
if (pkg != NULL) {
myname = strdup(str);
svt1 = newRV_noinc((SV *)myname);
svt2 = sv_bless(svt1, pkg);
}
ST(0) = svt2;

4. Create a file test.t with content

use MyPkg;

my $charref = MyPkg::new("I am a char string\n");

5. perl Makefile.PL

6. make

7. perl -Iblib/lib -Iblib/arch test.t


Thanks for your support,
Regards,
Naren.


--
Narendran Kumaragurunathan,
Design Verification Engineer,
TooMuch Semiconductor Solutions Pvt. Ltd.
www.toomuchsemi.com
A Bangalore based startup specialising on services in EDA & Verification.
Robert Jordan

2005-08-27, 6:55 pm

Naren,

> SV *
> new(const char *str)
> PREINIT:
> char* myname;
> SV* svt1;
> SV* svt2 = &PL_sv_undef;
> HV* pkg;
> CODE:
> pkg = gv_stashpv("MyPkg", 0);
> if (pkg != NULL) {
> myname = strdup(str);
> svt1 = newRV_noinc((SV *)myname);


This is totally broken. You're casting from char* to SV*.

Rob
Daniel Zinn

2005-08-27, 6:55 pm

Narendran Kumaraguru Nathan schrieb:

> I've been trying to write modules in 'c'....
> [ and there are problems ]


Perhaps you want to have a look at swig: http://www.swig.org

Best regards,
Daniel
narenkumaraguru@yahoo.co.uk

2005-08-27, 6:55 pm

Hi Rob,
I want to store the char * pointer. Will that cause any problem?? I
guess it should not! The RV contains the pointer value (char *) and I
guess it doesn't touch it at all. Please correct me if my understanding
is incorrect.

Thanks,
Naren.

Narendran Kumaraguru Nathan

2005-08-27, 6:55 pm

No Daniel, I've gone through swig before and decided to stick with XS.
Rgds,
Naren.

Daniel Zinn wrote:
> Narendran Kumaraguru Nathan schrieb:
>
>
>
>
> Perhaps you want to have a look at swig: http://www.swig.org
>
> Best regards,
> Daniel



--
Narendran Kumaragurunathan,
Design Verification Engineer,
TooMuch Semiconductor Solutions Pvt. Ltd.
www.toomuchsemi.com
A Bangalore based startup specialising on services in EDA & Verification.
Narendran Kumaraguru Nathan

2005-08-27, 6:55 pm

Narendran Kumaraguru Nathan wrote:
> Hi All,
> I've been trying to write modules in 'c'. I've gone through the
> documentation of perlguts, perlapi & perlxs. Although what I'm trying to
> do is much complicated to explain here, I am extracting out the problem
> with a small example.
> o I want to create an object (malloc'ed) and store the object's address
> as SV.
> o Once created & blessed, I should be able to call the function of the
> object by simply telling "$object->method(params)".
> I don't know what mistake I'm doing but I'm getting segmentation fault.
> Sometimes I get a message saying "Attempt to change a constant's value"
> or something like that. To replicate the problem do the following
>
> 1. h2xs -A -n MyPkg
> 2. cd MyPkg
> 3. Append the following code to MyPkg.xs file
> SV *
> new(const char *str)
> PREINIT:
> char* myname;
> SV* svt1;
> SV* svt2 = &PL_sv_undef;
> HV* pkg;
> CODE:
> pkg = gv_stashpv("MyPkg", 0);
> if (pkg != NULL) {
> myname = strdup(str);
> svt1 = newRV_noinc((SV *)myname);
> svt2 = sv_bless(svt1, pkg);
> }
> ST(0) = svt2;
>
> 4. Create a file test.t with content
>
> use MyPkg;
>
> my $charref = MyPkg::new("I am a char string\n");
>
> 5. perl Makefile.PL
>
> 6. make
>
> 7. perl -Iblib/lib -Iblib/arch test.t
>
>
> Thanks for your support,
> Regards,
> Naren.
>
>



Hi All,
Was able to solve the problem with this... Thanks for all your support!!


====CODE START====
SV *
new(const char *str)
PREINIT:
char* myname;
SV* svt1 = &PL_sv_undef;
SV* myrv;
CODE:
myname = strdup(str);
myrv = newRV_noinc(svt1);
svt1 = sv_setref_pv(myrv,"MyPkg",(void *)myname);
ST(0) = svt1;


void
print(SV* myref)
PREINIT:
char* str;
SV* svt1;
CODE:
str = (char *) SvUV(SvRV(myref));
fprintf(stderr, "I have:%s\n", str);
====CODE END====


--
Narendran Kumaragurunathan,
Design Verification Engineer,
TooMuch Semiconductor Solutions Pvt. Ltd.
www.toomuchsemi.com
A Bangalore based startup specialising on services in EDA & Verification.
Daniel Zinn

2005-08-27, 6:55 pm

Narendran Kumaraguru Nathan schrieb:

> No Daniel, I've gone through swig before and decided to stick with XS.


OK, I only thought just in case you do not know about swig.

Regards,
Daniel

Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com