Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

Does it possible to run an outer program in perl?

( #15587=categorized question: print w/ replies, xml ) Need Help??
Contributed by JJ on May 31, 2000 at 10:20 UTC
Q&A  > programs and processes


Description:

I want to introduct software like "verilog" in perl. So can I setup a env of my own. Is there a way to do it?

Answer: Does it possible to run an outer program in perl?
contributed by Corion

Perl can of course start other programs. Depending on whether you want to just start the other program and let your script finish or start the program and capture output and maybe start another program after that, there are many options for you to use :

  • exec() abandons the current program and starts another program
  • system() starts the other program, waits for it to complete its run and then continues your current program
  • fork() starts the current program a second time so that you have two instances of your program running

The man pages have examples for every one of these possibilities, but the one you will be most likely looking for is exec(), as all the other functions can be simulated using exec().

If you simply want to start another program with a fixed command line, wait for it to finish and capture all output, you can use the following code (untested) :

$output = `/path/to/program -and -some -parameters`;

Note that, depending on the operating system and shell, not everything will work as expected. Under Win32, you can't start Perl scripts directly, you have to start perl.exe and give -wT script.pl as the command line parameters. The line given to `` will also be subject to UNIX shell wildcard expansion, so you have to be very carefull in security sensitive situations when using variables in the `` part. A short example :

$filename = @ARGV[1]; $output = `/bin/program $filename`;
If a user then gives (for example) "/dev/null; rm -rf /* &" as the first parameter, the shell will start rm as a second process, which might be a security hole when using this in a CGI application.

Answer: Does it possible to run an outer program in perl?
contributed by damian1301

See How do I run another program from within my Perl program. It's like your question.

Please (register and) log in if you wish to add an answer



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • 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, 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, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • Outside of code tags, you may need to use entities for some characters:
            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.
  • Log In?
    Username:
    Password:

    What's my password?
    Create A New User
    Chatterbox?
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others avoiding work at the Monastery: (9)
    As of 2014-07-31 20:52 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      My favorite superfluous repetitious redundant duplicative phrase is:









      Results (253 votes), past polls