You could use system (with STDERR/STDOUT redirected) or popen to communicate with an external perl process.

The system() approach is more batch-like. You can later open (or tail) the output from the redirected streams. popen() would allow you to interact with the external perl process (reading/writing) while it is executed.

Further issues to handle: buffering, timeouts, return-codes, security, permissions, ...
Something along this demo:

#include <stdio.h> #include <string.h> int main() { /* simulation: calling an external Perl module */ char *cmd = "/usr/bin/perl -e ' print q{PerlEcho: }, join(q{, },@A +RGV), qq{\n} '"; /* first example: system() */ char sys_cmd[300]; /* hey, just a demo ... */ strcpy(sys_cmd, cmd); strcat(sys_cmd, " par1 par2 par3 its the 'system()' call "); /* a realistic example would redirect STDOUT and STDERR into files + */ /* e.g: strcat(sys_cmd," par1 par2 >output.1 2>output.2"); + */ /* (shell-interpreted: standard disclaimers apply) */ printf("Calling perl via system():\n>>>"); fflush(stdout); system(sys_cmd); /* todo: check result */ /* second example: popen() */ char ppn_cmd[300]; strcpy(ppn_cmd, cmd); strcat(ppn_cmd, " now via 'popen()' "); printf("Calling perl via popen():\n>>>"); FILE *fh = popen(ppn_cmd, "r"); /* read-only for now */ if ( fh ) { char buffer[101]; while( !feof(fh) ) { int len = fread(buffer, 1, 100, fh); if ( len>0 ) { buffer[len] = '\0'; printf("%d: %s",len,buffer); } } if ( pclose(fh) ) perror("ERROR - Failed to close pipe properl +y"); } else { perror("cannot popen"); printf("ERROR - cannot run popen(%s,\"r\",)\n", ppn_cmd); } return 0; } /* pb:sopw > gcc sys_n_popen.c && a.out Calling perl via system(): >>>PerlEcho: par1, par2, par3, its, the, system(), call Calling perl via popen(): >>>28: PerlEcho: now, via, popen() */

In reply to Re: calling perl from C/C++ by Perlbotics
in thread calling perl from C/C++ by perl_fan

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.