Ganeshgm7
Ganeshgm7

Reputation: 455

Perl Variable Concatenation

I am trying this code, but the concatenation was not done.

$host=qx(hostname -s);
my $day=qx(date +%A);
$fileType="$host-$day.tgz";
print $fileType;

When I execute this, I'll get output like, one by one

ldap
-Thursday
.tgz

But, I need a output like ldap-Thursday.tgz

Upvotes: 1

Views: 51

Answers (1)

choroba
choroba

Reputation: 242373

qx returns the string including the final newline. Remove it with chomp:

$host = qx{hostname -s};
chomp $host;

Note that Sys::Hostname provides a hostname subroutine which already removes any unwanted whitespace.

Similarly, you can use localtime->fullday with Time::Piece instead of calling date +%A.

Upvotes: 4

Related Questions