Re: Invoking a perl program via an executable c++ program...
by trantor (Chaplain) on Nov 14, 2001 at 20:11 UTC
|
It is easily accomplished using the system() function (which is available in C, C++ and basically every other language, sometimes with a different name and/or namespace) #included from stdlib.b
Perhaps it is even better to make your perl program easier to use, without the need of any wrappers? :-) Unless you're exercising your C/C++ skills in string manipulation and program invocation of course.
-- TMTOWTDI
| [reply] [d/l] [select] |
|
I am brushing up on my C/C++ and I am trying to run Perl scripts to do some stuff in my C/C++ program. So what I am wondering how does system in C/C++ allow you to capture the output of that script? Or do I need to keep digging into popen?
Thanks
--BigJoe
Learn patience, you must. Young PerlMonk, craves Not these things. Use the source Luke.
| [reply] |
|
popen is really much simpler than what you think, just have a look at the manpage.
If you want to use system, keep in mind that it does not return the output of the program you want to run, but since the command is interpreted by the shell you can actually redirect stdout and/or stderr with something like:
system("command -options args > outfile");
and then read outfile
A third, much more exciting alternative, is to play with pipe, fork, dup, exec and do yourself what popen does. After this, you'll treat shell writers with much more respect :-)
-- TMTOWTDI
| [reply] [d/l] [select] |
|
Thanks :)
At the moment, the perl program I've coded is invoked at the command line. I want to make it easier for others to use it by double-clicking a c++ program with the extension .exe. Can I do that with just perl itself or do I need to use c++ to write the so called interface?
| [reply] |
|
| [reply] |
|
If you're using ActiveState's Perl distro, under the Perl\bin dir there's a runperl.bat. If you copy that file to the same directory that your perl script is in, and rename the .bat file to have the same name as the perl script (ex: if your perl script is 'program.pl', rename the .bat file to 'program.bat') - then someone at a command prompt should be able to just type 'program' or whatever the name of the script is to execute it.
Of course, windows should have associated the .pl extension to Perl as John mentioned, so you should just be able to double-click on the file itself. in windows. :)
~Brian
| [reply] |
|
|
Re: Invoking a perl program via an executable c++ program...
by MZSanford (Curate) on Nov 14, 2001 at 20:07 UTC
|
I will assume you want to capture your perl programs output. In that case, also not being a C++ expert, i would look to <code lang="C++">popen()</code>
i had a memory leak once, and it ruined my favorite shirt. | [reply] |