[url]http://www.perlmonks.org/?node_id=57193[/url]
system("wc -l");
Will call the given command and return the return value of that command. This is usually not what you want, because most of the times wc -l will mean that you want to get the number of lines back from that call and not if that call was successful or not.
$nol = `wc -l`
The backticks call the command and return it's output into the variable (here $nol. In this case this will be what you want.
Another way of doing this is to use
$nol = qx/wc -l/;
(mnemonic: qx quote execute). I think is just the same as the backquotes (at least I don't know any difference)
Of course there are other ways (exec,fork) that behave different with respect to processes, but I don't know much about this.
And use $? to check the error code of backticks of system().
system("wc -l");
Will call the given command and return the return value of that command. This is usually not what you want, because most of the times wc -l will mean that you want to get the number of lines back from that call and not if that call was successful or not.
$nol = `wc -l`
The backticks call the command and return it's output into the variable (here $nol. In this case this will be what you want.
Another way of doing this is to use
$nol = qx/wc -l/;
(mnemonic: qx quote execute). I think is just the same as the backquotes (at least I don't know any difference)
Of course there are other ways (exec,fork) that behave different with respect to processes, but I don't know much about this.
And use $? to check the error code of backticks of system().
本文介绍了在Perl中使用不同的方法来执行外部命令并获取其输出。主要讨论了使用system函数与反引号(`)的区别,并简要提及了qx运算符的用法。通过实例说明如何正确地获取wc -l命令的输出。
2942

被折叠的 条评论
为什么被折叠?



