[英]How can I store Perl's system function output to a variable?
I have a problem with the system function. 我的系统功能有问题。 I want to store the system functions output to a variable. 我想将系统功能输出存储到变量中。
For example, 例如,
system("ls");
Here I want all the file names in the current directory to store in a variable. 在这里,我希望当前目录中的所有文件名都存储在一个变量中。 I know that I can do this by redirecting the output into a file and read from that and store that to a variable. 我知道我可以通过将输出重定向到文件中并从中读取并将其存储到变量中来做到这一点。 But I want a efficient way than that. 但是我想要一种有效的方法。 Is there any way . 有什么办法吗。
不,您不能存储ls输出的值,因为系统始终将命令作为子进程执行,所以请尝试使用backtick`command`,它在当前进程本身中执行命令!
The easiest way uses backticks or qx()
: 最简单的方法是使用反引号或qx()
:
my $value = qx(ls);
print $value;
The output is similar to the ls
. 输出类似于ls
。
My answer does not address your problem. 我的回答没有解决您的问题。 However, if you REALLY want to do directory listing, don't call system ls
like that. 但是,如果您确实要列出目录,请不要这样调用系统ls
。 Use opendir() , readdir() , or a while
loop. 使用opendir() , readdir()或while
循环。
For example, 例如,
while (<*>){
print $_ ."\n";
}
In fact, if it's not a third-party proprietary program, always try to user Perl's own functions. 实际上,如果它不是第三方专有程序,请始终尝试使用Perl自己的功能。
As abubacker stated, you can use backticks to capture the output of a program into a variable for later use. 如abubacker所述,您可以使用反引号将程序的输出捕获到变量中以供以后使用。 However, if you also need to check for exceptional return values, or bypass invoking the shell, it's time to bring in a CPAN module, IPC::System::Simple : 但是,如果您还需要检查异常的返回值,或者绕过调用Shell,那么该是时候引入CPAN模块IPC :: System :: Simple了 :
use IPC::System::Simple qw(capture);
# Capture output into $result and throw exception on failure
my $result = capture("some_command");
This module can be called in a variety of ways, and allows you to customize which error return values are "acceptable", whether to bypass the shell or not, and how to handle grouping of arguments. 可以通过多种方式调用此模块,并允许您自定义哪些错误返回值是“可接受的”,是否绕过外壳以及如何处理参数分组。 It also provides a drop-in replacement for system()
which adds more error-checking. 它还提供了对system()
替换,从而增加了更多的错误检查。
The official Perl documentation for the built-in system function states: 内置系统功能的官方Perl文档指出:
This is not what you want to use to capture the output from a command, for that you should use merely backticks or qx//, as described in "
STRING
" in perlop. 这不是您要用来捕获命令输出的内容,因为您应该仅使用反引号或qx //,如perlop中的“STRING
”中所述。
There are numerous ways to easily access the docs: 有多种方法可以轻松访问文档:
perldoc -f system
在命令行上: perldoc -f system
If you want each directory listing stored into a separate array element, use: 如果要将每个目录列表存储到单独的数组元素中,请使用:
my @entries = qx(ls);
使用反引号将输出存储在变量中
$output = `ls`;
A quick and simple way to do this is to use qx()
specifically for your example: 一种快速而简单的方法是针对您的示例使用qx()
:
my $output = qx(ls 2>&1);
The 2>&1
part is to capture both stdout and stderr. 2>&1
部分用于捕获stdout和stderr。
Since it has not been mentioned by other answers yet, you can also use Capture::Tiny to store any arbitrary STDOUT (and/or STDERR) into a variable, including from the system command. 由于尚未被其他答案提及,因此您还可以使用Capture :: Tiny将任意STDOUT(和/或STDERR)存储到变量中,包括从系统命令中存储。
use strict;
use warnings;
use Capture::Tiny 'capture_stdout';
my ($stdout, $return) = capture_stdout { system 'ls' };
# error checking for system return value required here
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.