Hi. I'm new to Perl XS. I have worked on perl for years.But i have only basic knowledge in C. I'm trying to create a C program say test.c and inside it I have a print array function which accepts an array and loops and print. I have created a corresponding test.xs file. and .pm file. I have made and installed it. When I try to use it it works for char array , but when I print an int array.. it doesn't. Please find the code below
#!/usr/bin/env perl use ExtUtils::testlib; use Data::Dumper; use myClibs; print "Compiling C program...\n"; print qx{compile.sh}; my $test = [ 1, 2, 3, 4, 5, 6, 9]; ArrayTest::print_array_char( "revendar" ); print "------------------------------------------------\n"; ArrayTest::print_array_char( @$test ); print "------------------------------------------------\n"; ArrayTest::print_array_int( "revendar",8 ); print "------------------------------------------------\n"; ArrayTest::print_array_int( @$test ,8 );
C prog is functions.c
#include <stdio.h> #include <string.h> #include <math.h> void print_array_char(char * array) { int l; int i; l=strlen(array); printf("Length of array is %d\n",l); for(i=0;i < l;i++) { printf("Element array[%d] = %c\n",i,array[i]); } } void print_array_int(const int array[], int l) { int i; for(i=0;i < l;i++) { printf("Element array[%d] = %d\n",i,array[i]); } }
xs file is myClibs.xs
#include "EXTERN.h" #include "perl.h" #include "XSUB.h" #include "ppport.h" MODULE = myClibs PACKAGE = ArrayTest PROTOTYPES: ENABLE #include <stdio.h> #include <string.h> #include <math.h> void print_array_char(array) char * array void print_array_int(array,l) char * array int l
pm file is myClibs.pm
package myClibs; our $VERSION = '0.01'; require XSLoader; XSLoader::load('myClibs', $VERSION); 1;
My question is.. Can I not just write a complex C program and write a simple xs file containing the function/datatype names and compile it. Do I need to learn perlguts to achieve what I want. I'm trying to write a c program to replace a perl function which is slow and handles huge data. I am at the gates waiting for a solution/advice
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |