Click here to get back home

writing modules in 'c'

 HomeNewsGroups | Search | About
 comp.lang.perl.modules    Post an article   get this group's latest topics as an RSS feed add this group's latest topics to your My MSN content add this group's latest topics to your My Yahoo content
Subject Author Date
writing modules in 'c' Narendran Kumaraguru Nathan 08-27-2005
Get Chitika Premium
Posted by Narendran Kumaraguru Nathan on August 27, 2005, 3:18 pm
Please log in for more thread options


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.


Posted by Robert Jordan on August 27, 2005, 4:15 pm
Please log in for more thread options


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


Posted by Daniel Zinn on August 27, 2005, 5:04 pm
Please log in for more thread options


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


Posted by Narendran Kumaraguru Nathan on August 27, 2005, 10:09 pm
Please log in for more thread options


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

Daniel Zinn wrote:
> 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


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


Posted by Daniel Zinn on August 27, 2005, 10:39 pm
Please log in for more thread options


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



Similar ThreadsPosted
Writing row at a time in Excel using OLE June 14, 2007, 1:43 pm
Writing VB macro script in xls May 20, 2008, 7:46 am
Reading AND writing Excel spreadsheets April 30, 2005, 10:05 am
what module to use when writing automation tests? August 25, 2008, 12:37 am
flush writing to multiple children IO::Pipe April 29, 2005, 12:36 pm
Writing a HTML/ASP SOAP client for a SOAP::Lite destination March 16, 2005, 5:19 pm
GD modules September 1, 2004, 8:20 am
Perl PDF modules - help please August 11, 2004, 2:31 pm
installing modules? September 29, 2005, 1:52 pm
Need new maintainers for my modules January 8, 2005, 7:24 am

Our other projects:

Art Dolls, Fairies and Mermaids - Sunnyfaces.net

Roy's Linux, Programming and Search Engines messages

1-Script XML SitemapXML Sitemap