Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

Re: Does it possible to run an outer program in perl?

by Corion (Patriarch)
on May 31, 2000 at 12:37 UTC ( [id://15596]=note: print w/replies, xml ) Need Help??


in reply to Does it possible to run an outer program in perl?

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.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others pondering the Monastery: (6)
As of 2024-04-25 10:54 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found