I thought this must be simple, but I haven't found a viable solution.
The problem is as simple as this: I want to execute a system command and capture the output in Perl variable. The command is specified in Perl array (containing command and parameters, e.g. @cmd = ('mycmd', '-opt1', 'arg1', 'val1')).
I don't want to use forking, i.e. open(FROM_KID, '-|') is not an option. I know that if I had the command in a string I can achieve this with backticks. So perhaps this problem reduces to converting @cmd array into a string. In my case, the command arguments can contain spaces.
Is there a simple way to convert @cmd array into string that can be used with backticks, but such that all arguments are properly quoted? Also ideally without using any external libraries.
Thanks!!
systemor backtics will fork a child process - there's no way around that. You could do something like$cmd = join(' ', map { s/'/\\'/g; "'$_'" } @cmd);butopenwould probably be safer..system.systemand backticks.