Perl Variable Concatenation Problem

I just trying to learn PERL. I have trying this line, but the concatenation was not done.

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

ll Get a output like, one by one

ldap
-Thursday
.tgz

But I need a output like ldap-Thursday.tgz Please help me in this. Thanks in Advance.

You need to chomp the values for both variables before joining them into $fileType. Save the following code snippet to a file named example.pl:

#!/usr/bin/perl
use strict;
use warnings;
my $host = qx(hostname -s);
my $day = qx(date +%A);
chomp $host;
chomp $day;
my $fileType = "$host-$day.tgz";
print "$fileType\n";

Then run as

perl example.pl

Refer to the attached image for the result.

Hope it helps :smile:.

2 Likes

Thank you…! It’s working well…! Now i got the exact output…!

I am glad to hear that. Don’t hesitate to come back to LinuxSay.com each time you have a question. We look forward to helping you.

I just beginner in this field. Once I get a better knowledge then it will be useful to provide a valid answer. Surely I’ll be active after that. I’m frequently following this forum. It was so helpful to me. Thank you