Rajeev
Rajeev

Reputation: 46919

perl simple concatenate

In the following perl code how to concatenate pwd with a string. In the following the output is in two different lines

   $pwd=`pwd`;
   my $string = $pwd . "/.somename";
   print "$string";

Upvotes: 1

Views: 407

Answers (3)

summea
summea

Reputation: 7593

Use chomp(), like this:

my $pwd = `pwd`;
chomp($pwd);
my $string = $pwd . "/.somename";
print "$string";

http://perldoc.perl.org/functions/chomp.html

Upvotes: 1

Brian Roach
Brian Roach

Reputation: 76898

You just did. It just so happens there's a newline character in the middle.

$pwd=`pwd`;
chomp($pwd);
my $string = $pwd . "/.somename";
print "$string";

That being said ... don't do that. You should use cwd()

use Cwd;

my $string = cwd() . "/.somename";
print "$string\n";

Upvotes: 7

ZhangChn
ZhangChn

Reputation: 3184

$pwd=`pwd`;
chomp $pwd;
my $string = $pwd . "/.somename";
print $string;

Upvotes: 2

Related Questions