The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
Google UNIX.COM
Home Forums Register Rules & FAQ Members List Arcade Search Today's Posts Mark Forums Read


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts here. Shell Script Page.


Other UNIX.COM Threads You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
awk and fuction (recursion) !! Urgent !! varungupta UNIX for Advanced & Expert Users 12 04-16-2008 02:12 PM
about memset fuction ranj@chn High Level Programming 3 01-31-2006 04:59 AM
perl "system" cmd return values.. sekar sundaram Shell Programming and Scripting 0 11-24-2005 04:00 AM
Get Oracle fuction return value in a variable rahulrathod UNIX for Dummies Questions & Answers 3 04-14-2005 09:50 AM
perl: why the return valure of stat and lstat are the same? gusla Shell Programming and Scripting 1 05-30-2002 10:58 AM

Reply
 
Submit Tools LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 05-14-2008
Registered User
 

Join Date: Jan 2008
Location: Bangalore,India
Posts: 126
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiReddit! Stumble this Post!Spurl this Post!
Arrow 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
Reply With Quote
Forum Sponsor
  #2 (permalink)  
Old 05-15-2008
era era is offline
Herder of Useless Cats
 

Join Date: Mar 2008
Location: /there/is/only/bin/sh
Posts: 2,203
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiReddit! Stumble this Post!Spurl this Post!
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.
Reply With Quote
  #3 (permalink)  
Old 05-15-2008
Registered User
 

Join Date: Jan 2008
Location: Bangalore,India
Posts: 126
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiReddit! Stumble this Post!Spurl this Post!
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.
Reply With Quote
  #4 (permalink)  
Old 05-15-2008
era era is offline
Herder of Useless Cats
 

Join Date: Mar 2008
Location: /there/is/only/bin/sh
Posts: 2,203
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiReddit! Stumble this Post!Spurl this Post!
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
Reply With Quote
  #5 (permalink)  
Old 05-15-2008
Registered User
 

Join Date: Jan 2008
Location: Bangalore,India
Posts: 126
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiReddit! Stumble this Post!Spurl this Post!
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 ..
Reply With Quote
Google UNIX.COM
Reply



Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT -7. The time now is 03:37 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008 The CEP Blog All Rights Reserved -Ad Management by RedTyger

Search Engine Optimization by vBSEO 3.1.0

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102