Basically, there are three ways to run an external prog - 1 - system. 2 - backticks. 3 - open a pipe.
system:
my $cmdToRun = "/path-to/the-c-prog";
system ($cmdToRun) && die "system $cmdToRun - $!\n";
'system' just returns 0 on success and 1 on failure therefore you should test for it.
backticks:
my $cmdToRun = "/path-to/the-c-prog";
my @results = `$cmdToRun`;
Using backticks is useful if you need to capture output from the program you are calling.
pipe:
my $cmdToRun = "/path-to/the-c-prog";
open (CMD, "$cmdToRun |") || die "open $cmdToRun - $!\n";
while (<CMD>) {
# loop through the program output here...
}
UPDATE Of course this assumes you meant calling an external c prog from within perl. After reading sniper's post I realize you could have meant running actual c code from within perl - in that case Inline.pm would be the natural choice.
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.
|