正如其他人所说, chomp 返回删除的字符数。在我的特定实例中(单行 perl 替换文件语句中带有 eval 修饰符的正则表达式),我需要在单个语句中获取 chomped 值,而无需单独的打印语句。我终于找到了一个可行的解决方案 - 将 chomp 命令包装在 if 语句中。
从...开始:
$in = "I have COMMITS commits in my log";
$in =~ s/COMMITS/`git log | grep -i '^commit' | wc -l`/e;
print $in;
这将返回:
I have 256
commits in my log
太好了,我需要大嚼这个,所以我尝试:
$in = "I have COMMITS commits in my log";
$in =~ s/COMMITS/chomp `git log | grep -i '^commit' | wc -l`/e;
print $in;
但这会引发错误:
Can't modify quoted execution (``, qx) in chomp at ./script line 4, near "`git log | grep -i '^commit' | wc -l`}"
Execution of ./script aborted due to compilation errors.
是的,所以我需要将输出分配给本地 var 并 chomp 它:
$in = "I have COMMITS commits in my log";
$in =~ s/COMMITS/chomp (my $VAR = `git log | grep -i '^commit' | wc -l`)/e;
print $in;
但正如我们所说, chomp 返回删除的字符数:
I have 1 commits in my log
然后我发现我可以将它包装在一个 if 语句中并让它返回结果,chomped:
$in = "I have COMMITS commits in my log";
$in =~ s/COMMITS/if (chomp (my $VAR = `git log | grep -i '^commit' | wc -l`)) { $VAR }/e;
print $in;
最后,我在一个语句中得到了命令结果,chomped:
I have 256 commits in my log