Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

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


In reply to Perl XS by revendar

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others meditating upon the Monastery: (6)
As of 2024-04-24 23:18 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found