如何在perl中将参数传递给系统命令 [英] how to pass arguments to system command in perl

查看:47
本文介绍了如何在perl中将参数传递给系统命令的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的命令:

system("start cmd.exe /k C:\\script.pl $arg1 $arg2 $arg3");

没有正确传递参数.这样做的正确方法是什么?

is not passing the arguments correctly. What is the correct way to do this?

谢谢

推荐答案

调用 system 的最佳方式是使用数组或列表:

The best way to invoke system is with an array or a list:

my @args = ("start", "cmd.exe", "/k", "C:\\script.pl", $arg1, $arg2, $arg3);
system @args;

system "start", "cmd.exe", "/k", "C:\\script.pl", $arg1, $arg2, $arg3;

system 的单个字符串相比,这节省了如何引用参数"的复杂性,因为 shell 没有机会解释它们.另一方面,如果您希望 shell 进行 I/O 重定向或管道传输,您可能不会使用这种机制.

Compared with a single string to system, this saves the complexities of 'how to quote the arguments' because the shell doesn't get a chance to interpret them. On the other hand, if you want the shell to do I/O redirection or piping, you probably won't use this mechanism.

这篇关于如何在perl中将参数传递给系统命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆