In Perl, to run another Perl script from my script, or to run any system commands like mv
, cp
, pkgadd
, pkgrm
, pkginfo
, rpm
etc, we can use the following:
system()
exec()
``
(Backticks)Are all the three the same, or are they different? Do all the three give the same result in every case? Are they used in different scenarios, like to call a Perl program we have to use system()
and for others we have to use ``
(backticks).
Please advise, as I am currently using system()
for all the calls.
They're all different, and the docs explain how they're different. Backticks capture and return output; system
returns an exit status, and exec
never returns at all if it's successful.
IPC::System::Simple is probably what you want.
It provides safe, portable alternatives to backticks, system() and other IPC commands.
It also allows you to avoid the shell for most of said commands, which can be helpful in some circumstances.
The best option is to use some module, either in the standard library or from CPAN, that does the job for you. It's going to be more portable, and possibly even faster for quick tasks (no forking to the system).
However, if that's not good enough for you, you can use one of those three, and no, they are not the same. Read the perldoc pages on system()
, exec()
, and backticks to see the difference.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With