Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

Invoking a perl program via an executable c++ program...

by kiat (Vicar)
on Nov 14, 2001 at 19:37 UTC ( [id://125327]=perlquestion: print w/replies, xml ) Need Help??

kiat has asked for the wisdom of the Perl Monks concerning the following question:

Hi, I've written a perl program that is invoked from the dos command line (i.e. c:\perl5.6\newbie\perl program.pl arg1 arg2). I'm new to c++ and have just written my first "hello world!" program with it. I would like to use c++ to write a simple interface to invoke the perl program I've written to make it easier to use. How do I do that? Is it easily accomplished? Thanks in advance :) kiat
  • Comment on Invoking a perl program via an executable c++ program...

Replies are listed 'Best First'.
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

      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.

        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

      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?
        You can double-click on files with .pl, .perl, .wperl, etc. if you want. The GUI shell should ask you what to do the first time you try.

        I'm guessing you want to hide the console output and have the wrapper use a Window instead? Use wperl (or is it perlw?) .exe instead of perl.exe to run the script.

        —John

        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
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.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://125327]
Approved by root
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others musing on the Monastery: (4)
As of 2025-05-16 18:13 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?
    erzuuliAnonymous Monks are no longer allowed to use Super Search, due to an excessive use of this resource by robots.