Can someone please help me? In Perl, what is the difference between:
exec "command";
and
system("command");
and
print `command`;
Are there other ways to run shell commands too?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
exec
executes a command and never returns.
It’s like a
returnstatement in a function.If the command is not found
execreturns false.It never returns true, because if the command is found it never returns at all.
There is also no point in returning
STDOUT,STDERRor exit status of the command.You can find documentation about it in
perlfunc,because it is a function.
system
executes a command and your Perl script is continued after the command has finished.
The return value is the exit status of the command.
You can find documentation about it in
perlfunc.backticks
like
systemexecutes a command and your perl script is continued after the command has finished.In contrary to
systemthe return value isSTDOUTof the command.qx//is equivalent to backticks.You can find documentation about it in
perlop, because unlikesystemandexecit is an operator.Other ways
What is missing from the above is a way to execute a command asynchronously.
That means your perl script and your command run simultaneously.
This can be accomplished with
open.It allows you to read
STDOUT/STDERRand write toSTDINof your command.It is platform dependent though.
There are also several modules which can ease this tasks.
There is
IPC::Open2andIPC::Open3andIPC::Run, as well asWin32::Process::Createif you are on windows.