简体   繁体   English

如何在Perl数组或标量中存储git命令的输出?

[英]How do I store output of a git command in a Perl array or scalar?

In my code I have 在我的代码中我有

git diff --numstat

I know I could create a file with 我知道我可以创建一个文件

git diff --numstat > log.log

But is it even possible to pass this into an array or scalar of some sort? 但是甚至可以将它传递给某种数组或标量? I was thinking something like this but I'm not sure why it doesn't compile. 我在想这样的事情,但我不确定它为什么不编译。

my @array;
push (@array, git diff --numstat);

Use backticks, also known more generally as qx// : 使用反引号,也称为qx //

  • qx/STRING/
  • `STRING`
    A string which is (possibly) interpolated and then executed as a system command with /bin/sh or its equivalent. 一个字符串(可能)插入然后作为系统命令执行,带有/bin/sh或其等价物。 Shell wildcards, pipes, and redirections will be honored. 壳牌通配符,管道和重定向将受到尊重。 The collected standard output of the command is returned; 返回收集的命令标准输出; standard error is unaffected. 标准错误不受影响。 In scalar context, it comes back as a single (potentially multi-line) string, or undef if the command failed. 在标量上下文中,它作为单个(可能是多行)字符串返回,如果命令失败则返回undef In list context, returns a list of lines (however you've defined lines with $/ or $INPUT_RECORD_SEPARATOR ), or an empty list if the command failed. 在列表上下文中,返回行列表(但是您已使用$/$INPUT_RECORD_SEPARATOR定义行),如果命令失败,则返回空列表。

You have options, and which is better depends on what you want to do with the output. 您有选项,哪个更好取决于您想要对输出做什么。

To read all of the standard output into a scalar, use the operator in scalar context as in 要将所有标准输出读入标量,请在标量上下文中使用运算符

$output = `git diff --numstat`;

In list context with the default value of $/ , perl splits the output into separate lines. 在列表上下文中,默认值为$/ ,perl将输出拆分为单独的行。 If you want to append the git output to the end of an existing array, use push , as in 如果要将git输出附加到现有数组的末尾,请使用push ,如下所示

push @array, `git diff --numstat`;

Although you mentioned push specifically in your question, I'm having a hard time imagining why you'd mix the output of git with something else. 虽然你在你的问题中特别提到了push特,但是我很难想象你为什么要把git的输出与其他东西混合在一起。 Storing the output in an array directly is simpler: 将输出直接存储在数组中更简单:

@array = `git diff --numstat`;

Note that the list of lines returned retain their end-of-line characters. 请注意,返回的行列表保留其行尾字符。 To create a new array and remove all of the newlines in one line, write 要创建一个新数组并删除一行中的所有换行符,请写入

chomp(@array = `git diff --numstat`);

or even 甚至

chomp(my @array = `git diff --numstat`);

if you're running under use strict . 如果你在use strict下运行

Error Handling 错误处理

For code that you plan to use more than once or twice, you should check that `git diff --numstat` , or any other command whose output you want to read, actually succeeded. 对于计划使用多次或两次的代码,您应该检查`git diff --numstat` ,或者您要读取其输出的任何其他命令实际上是否成功。 Otherwise, with the warnings pragma enabled, you'll see lots of diagnostic messages about undefined variables or missing output. 否则,启用warnings编译指示后,您将看到许多有关未定义变量或缺少输出的诊断消息。

In scalar context, failure will return the undefined value. 在标量上下文中,失败将返回未定义的值。 Check it as in 检查它

my $output = `git diff --numstat`;
die "$0: git may not be installed" unless defined $output;

Failure in list context produces an empty list. 列表上下文中的失败会生成一个空列表。

my @output = `git diff --numstat`;
die "$0: git may not be installed" unless @output;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM