Reputation: 5225
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.
Upvotes: 0
Views: 7417
Reputation: 47042
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.
Upvotes: 5
Reputation:
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!
Upvotes: 5
Reputation: 2115
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.
Upvotes: -1