fuction return in perl


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting fuction return in perl
# 1  
Old 05-15-2008
CPU & Memory 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
# 2  
Old 05-15-2008
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.
# 3  
Old 05-15-2008
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.
# 4  
Old 05-15-2008
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
# 5  
Old 05-15-2008
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 ..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Problem saving return value of subroutine in perl

Hi all, I have this code #This program read the triplets from file named "data" into #an array of array. use strict; use warnings; use Data::Dumper; use Graph; use Graph::Subgraph; my @S; while (<>) { push @S, ; } print "-----TRIPLETS-------\n"; print Dumper \@S; #Make... (6 Replies)
Discussion started by: rushadrena
6 Replies

2. Shell Programming and Scripting

Perl system fuction with

Hi, I've written a perl script with a nested foreach statement in it. I have the line code: foreach $argn(@arguments) { foreach $location (@path) { system("find $location -name \"$argn\" -print >> results.txt"); ... (2 Replies)
Discussion started by: tchoruma
2 Replies

3. Shell Programming and Scripting

Perl help - Searching for a pattern and return the position

Hi, I need to search a file, in each line I need to check for occurance of '1' from a particular position through the next 32 bytes. If 1 is found, i need to return the position. Here is an example of the file and the output i need. Please help. I'm new to perl and unix. File: ... (1 Reply)
Discussion started by: gpaulose
1 Replies

4. Shell Programming and Scripting

capturing C++ binary return status in perl

Hello, I have a C++ binary that runs in my perl script. But, Currently, the binary is doing a core dump and i want to capture the retrun status of the binary to report as an issue. Can you please help me on this. Thanks, Sateesh (1 Reply)
Discussion started by: kotasateesh
1 Replies

5. Shell Programming and Scripting

capturing C++ binary return status in perl

Hello, I have a C++ binary that runs in my perl script. But, Currently, the binary is doing a core dump and i want to capture the retrun status of the binary to report as an issue. Can you please help me on this. Thanks, Sateesh (1 Reply)
Discussion started by: kotasateesh
1 Replies

6. Shell Programming and Scripting

perl : stdout is not return to screen

Hello All, I have a perl script , and the STDERR and additional FH is redirected to the STDOUT like below: open STDOUT ,">>$log" or die "$! :: $log\n"; open STDERR ,">&STDOUT" or die "$! :: Can redirect STDERR to STDOUT\n"; select STDERR; $|=1; open LOG ,">&STDOUT" or die "$! :: Can... (2 Replies)
Discussion started by: Alalush
2 Replies

7. UNIX for Advanced & Expert Users

awk and fuction (recursion) !! Urgent !!

Hey all, :D Could you please check following way of writing awk is correct or not ??? ----------------------------------------------------------- ----------------------------------------------------------------- Its recursion being called. tempgrep.txt has : 462948 1311040 880922... (12 Replies)
Discussion started by: varungupta
12 Replies

8. Programming

about memset fuction

Dear all, In my code,i am planning to use memset function to re-initialise an array before populating it everytime. Will using memset function be an overload to the program? (3 Replies)
Discussion started by: ranj@chn
3 Replies

9. UNIX for Dummies Questions & Answers

Get Oracle fuction return value in a variable

Hi All, :confused: I have the following code. var=' ' sqlplus user/pass@DB <<EOF whenever sqlerror exit 1 select package.func() into $var from dual; EOF echo $var But, this code does not work to display the value returned by the oracle function. Do we have to bind variables before... (3 Replies)
Discussion started by: rahulrathod
3 Replies

10. Shell Programming and Scripting

perl: why the return valure of stat and lstat are the same?

i tried to use stat to get the attributes of a file and a soft link. but the result i got from stat and lstat are the same. say: ln -s f1 soft1 (soft is a soft link , point to f1) if i use > ls -il shows the inode and modify time of soft1 and f1 are different. but the modify... (1 Reply)
Discussion started by: gusla
1 Replies
Login or Register to Ask a Question