user3616128
user3616128

Reputation: 377

Unable to remove new line from the end of word in Perl

I have the script as:

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";

This gives me the output as:

NAME OF COMMENTOR  ************-> "Shalini Saini
" 

I.e Shalini Saini and followed by a new line. Instead of

NAME OF COMMENTOR  ************-> "Shalini Saini"

Why there is a new line after Saini and why do the quotes come in the next line? How can I trim it?

Upvotes: 1

Views: 80

Answers (1)

TLP
TLP

Reputation: 67910

Short answer: Remove the -l switch, because it causes print to have a newline at the end.

Long answer: Don't use a shell command to run Perl inside Perl, it's quite redundant. Just read the file normally.

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";

Upvotes: 6

Related Questions