tags:

views:

154

answers:

3

I want to execute a C program using Perl script. What ever inputs are given to the C executable manually, those should be given by my program..

Lets take a simple program, which takes input of two nos. and prints the sum of it. The values should be provided by Perl script.

Kindly guide me through some tutorial where I can achieve the same.

+3  A: 

Use the system function:

system "my-c-program 1 2";

If you want to capture the output from the C program in your perl script, then use backticks or the qx// operator:

my $output = `my-c-program 1 2`;

That runs my-c-program 1 2 and captures the standard output into a new $output variable.

Dave Hinton
I think the OP wants the output of the invoked program.
Sinan Ünür
U need to guide me thru this process.. As I am a newbie in this.. Or share some good tutorial on it.
AGeek
@RBA No. No one ought to need to guide you through this. It's a mere function call.
Sinan Ünür
A: 

You're probably after the backticks quoting mechanism, that executes an external program and returns its stdout as a string. e.g.

$date = `date`
print $date;

would print something like "Wed Oct 7 12:50:33 CEST 2009" in unix. However, the arguments must be shell-escaped for security purposes, and that can be tricky in certain scenarios -- tainting is the way to go in most cases.

I recommend all beginners to go directly for the 'system' command until they are aware of the security implications of the backticks -- if this is the case you should probably take @Dave Hinton's advice

For advanced magic you should read the perlipc perldoc.

codehead
`system` and `qx//` do different things. What does "*go directly for the `system` command until they are aware of the security implications of the backticks*" that mean? Incidentally, `system` is a function.
Sinan Ünür
It means that beginners should stay away from the backticks until they learn about shell escaping. And yes, you are right about system.
codehead
+4  A: 

Well, if you're just learning how to run external programs in Perl - please do yourself a favor and forget about ``.

The problem with `` is you execute a string with arguments in it - so it has to be parsed. And this might lead to issues when parameters are provided by user.

If you are 100% certain that you have full control over parameters, and command name - you can use ``, but for any other situation - consider using IPC::Run.

It is a bit more complex, but the single fact that it doesn't require any argument parsing makes is so much better. Plus you have full control over stdin, stdout and stderr of executed program - including attaching callbacks to them!

depesz
Hi Sir.. Ur answer look bit interesting too me.. But it sounds too much extra,, bcoz i ws not clear of sm terminologies.. How can i run a simple C program through perl script.. I.e. during execution when the input is demanded by C program, then perl should provide that input.. With time delays in b/w...
AGeek
I haven't understood ur comment?? Kindly write it again and clearly, if possible.. Anywys thnkx.
AGeek