PDA

View Full Version : Inline module : perl and c Inerface ?


ganesh_mak
01-30-2007, 12:50 PM
Hi,
can somebody give me the example code for this

i need a perl script which uses inlines module for c interface.

in .c file i need a function which gets the list of names in to char names[256][256]
and returns the list of names to the perl @names variable.

i just tried this code but its not working

function getNames() in c returns a ref of the array.so the lookNames() in perl does not return a list of names (array) .
here replace the getNames function in suchway that it returns an array and it should be assigned to the perl array @names;


test.pl
-------------------------------------------------

#! /usr/bin/perl
#use Inline C;

my @name_ref =&lookNames();


__C__

#include <names.c>

char* lookNames()
{
char *x;
x = getNames();
return x;
}


names.c
---------------------------------------


#include <stdio.h>
char* getNames()
{
char names[256][256];
strcpy(names[0],"cf");
strcpy(names[1],"cf1");
strcpy(names[2],"cf2");
return names;
}

KevinADC
01-30-2007, 07:51 PM
http://search.cpan.org/~ingy/Inline-0.44/C/C.pod

ralph l mayo
02-12-2007, 09:53 AM
Hi,
can somebody give me the example code for this

i need a perl script which uses inlines module for c interface.

in .c file i need a function which gets the list of names in to char names[256][256]
and returns the list of names to the perl @names variable.

i just tried this code but its not working

function getNames() in c returns a ref of the array.so the lookNames() in perl does not return a list of names (array) .
here replace the getNames function in suchway that it returns an array and it should be assigned to the perl array @names;


test.pl
-------------------------------------------------

#! /usr/bin/perl
#use Inline C;

my @name_ref =&lookNames();


__C__

#include <names.c>

char* lookNames()
{
char *x;
x = getNames();
return x;
}


names.c
---------------------------------------


#include <stdio.h>
char* getNames()
{
char names[256][256];
strcpy(names[0],"cf");
strcpy(names[1],"cf1");
strcpy(names[2],"cf2");
return names;
}



What compiler allows this C code?

char* getNames actually returns char**, and it returns a reference to a local (automatically scoped) variable so the results of using it will be between undefined and disasterous.

I'm strictly a C++ guy so I'd probably use vector<string> for this and my interpretation probably is nowhere near idiomatic C gospel but I think you need to do something similar to this (and then sort the inlining issues):


#include <stdio.h>
#include <stdlib.h>

char** getNames()
{
char** names = malloc(256*256);
if (!names) exit(1);
names[0] = "cf";
names[1] = "cf1";
names[2] = "cf2";
return names;
}

char** lookNames()
{
return getNames();
}


Note you're going to need to figure out how to get perl to free that pointer... no tips on that, as I haven't tried it.