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

Hi Everyone I've written a perl script which will iterate a command line execution for different inputs to produce multiple outputs.
foreach $filename(@filename) { [operator] ./DynDom $filename ; }
but what is the best operator to do this command line execution, I read that system(), exec() and ` ` all do it, but which one is preferable? Many Thanks in Advance!

Replies are listed 'Best First'.
Re: Command Terminal: system(), exec() etc query
by Corion (Patriarch) on Jan 28, 2011 at 14:37 UTC

    Read the documentation for them all. They all do different things. Use the one that is most appropriate.

Re: Command Terminal: system(), exec() etc query
by jethro (Monsignor) on Jan 28, 2011 at 14:43 UTC

    There is a command line utility named perldoc with which you can read perl documentation (and the documentation is also on the internet, if you prefer googeling. See http://perldoc.perl.org). If, for example, you do

    perldoc -f exec

    it conveniently jumps to where the function is explained. And the first sentence there will tell you that "exec executes a system command and never returns"

    If you read the other two (and I'll concede that the backticks documentation isn't that easy to find with perldoc), you will find out that the biggest difference between system and the backticks is that the backticks return the output of the system command and system does not

Re: Command Terminal: system(), exec() etc query
by lyklev (Pilgrim) on Jan 28, 2011 at 15:51 UTC

    Without knowing too much about your problem, I think you need

    system("./DynDom $filename")

    This will print the output of your program DynDom to the screen; you don't seem to want to capture the output in a variable, but if you do, you can then use

    my $output = `./DynDom $filename`;

    Exec is more tricky because it does not return to perl, it stops your program (in this case Perl), replaces perl with the program executed, then starts the new program. This means that it will do the first iteration in the loop, run your program and then exit.

    When you need exec(), you will know when you need it...