On 01/24/2015 09:13 AM, Mike wrote:
Which is generally considered the best practice way of executing system commands from within a Perl program?

Should I use backticks or qx//, system(), or exec()?

Or is there no "best way" and it's just a matter of what you want to return?

even though those are all related they are very different animals. the choice of system vs backticks is a classic newbie trap. they both run a subprocess but backticks collects the stdout of the process and returns it. system just lets stdout of the process be output to the existing stdout (usually the terminal). qx// and backticks are the exact same operation but with different syntax. it is like qq// is the same as double quotes but you can select the delimiter.

exec is rarely needed in general coding and even more rarely needed by newbies. it runs another process but that one REPLACES the current one which is usually what you don't want. exec is usually called in pairing with fork which will created a new child process. backticks and system both do a fork/exec call internally for you. if you think you need to call exec, you are likely wrong in choosing it. like with other dangerous perl things (symrefs, string eval), it is only to be used when you know when you should not use it. existing functions (as mentioned above) and some modules use exec correctly so use those first before you ever contemplate using exec.

uri

--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/


Reply via email to