无法从Perl中的单词末尾删除新行

use*_*128 1 perl

我有脚本:

my $workItemCommentorURL = "https://almweb1.ipc.com/jts/users/sainis";
my $json_text= "curl -s -D - -k  -b ~/.jazzcookies -o commentor.json -H \"Accept: application/x-oslc-cm*\" \"$workItemCommentorURL\"";
my $content = `cat commentor.json`;
my $WICommentCreator = `cat commentor.json | perl -l -ne '/<j.0:name>(.*)<\\/j.0:name>/ and print \$1'`;
print "NAME OF COMMENTOR  ************-> \"$WICommentCreator\"\n";
Run Code Online (Sandbox Code Playgroud)

这给了我输出:

NAME OF COMMENTOR  ************-> "Shalini Saini
" 
Run Code Online (Sandbox Code Playgroud)

即Shalini Saini,接着是一条新线.代替

NAME OF COMMENTOR  ************-> "Shalini Saini"
Run Code Online (Sandbox Code Playgroud)

为什么Saini之后会出现一条新线,为什么报价会出现在下一行?我怎么修剪它?

TLP*_*TLP 6

简短回答:删除-l开关,因为它导致最后print有一个换行符.

答案很长:不要使用shell命令在Perl中运行Perl,这是多余的.只需正常阅读文件即可.

use strict;
use warnings;

my $workItemCommentorURL = "https://almweb1.ipc.com/jts/users/sainis";
my $json_text= "curl -s -D - -k  -b ~/.jazzcookies -o commentor.json -H \"Accept: application/x-oslc-cm*\" \"$workItemCommentorURL\"";
my $content = do { 
    open my $fh, "<", "commentor.json" or die $!;
    local $/; <$fh>;
};
my ($WICommentCreator) = $content =~ /<j.0:name>(.*)<\/j.0:name>/;
print "NAME OF COMMENTOR  ************-> \"$WICommentCreator\"\n";
Run Code Online (Sandbox Code Playgroud)