fuction return in perl

Hi All,

I have a perl script(1.pl) that calls a c function defined in another file sample.c

#!/usr/bin/perl

my $re = 1;
my @s = `/home/PERL_SCRIPTING/Rough/sample pline $re 10`;
print "$_" foreach(@s);

The sample.c is as bwlow:

# include <stdio.h>
int pline(int, int);
main(int argc,char *argv[])
{
int i,j;

printf("Wow Entered main\n");
printf(" %s ", argv[1]);
printf(" %s ", argv[2]);
printf(" %s ", argv[3]);
if(strcmp(argv[1],"pline") == 0)
{
int i,j,k;
i = atoi(argv[2]);
j = atoi(argv[3]);
printf("value of i is %d \n",i);
k = pline(i,j);
printf("%d",k);

}
}

int pline( int x, int y)
{
x = x + 20;
printf("value of x : %d", x);
printf("\n");
return(x);
}

I can call the c function and values are printing. But i need only the value of k to be stored in a variable in the perl script so that i can use it later in teh script.

Is their any solution for this??
Thanks in advance
JS

You need to parse the output from your C program, or make its output format less complex. The fact that it might not always print a value for k seems like a slightly dubious design, if that's what it's primarily useful for. Also I'd centralize all the printing to one function, and make pline() only calculate and return the desired value (for printing or other use).

The last line of output will be in $s[-1] if that's the only output you are really interested in.

My actual code and function are different .
The code shown above is a simple example i need to implement.

The point is i need to return the value of k to the perl script a and store in a variable so tha ti can use it.

Printing it and having Perl capture the output is a perfectly good way to do that, or do you think otherwise?

In theory, you could make your C function callable directly from Perl, by wrapping it in something called XS. See man perlxstut

I tried [-1] and it worked .. Because its the only point available and needed.
Thanks a lot era ..

I dont know much about perl as am very new to this ..and my time is very limited that I cant do a leran and implement principle but implement and learn ..