Jessada Thutkawkorapin
Jessada Thutkawkorapin

Reputation: 1346

Is there a way to print the sum result in one line of code in perl?

Rather than summing the result in one line and printing it in another line? If I have this data

my $a = 1;
my $b = 2;

Currently, if I want to print the sum result, I need to write the code like this

my $tmp = $a+$b;
print "result=$tmp\n";

Is there to the same as above by something looking similar to

print "result=$a+$b\n";

Upvotes: 3

Views: 177

Answers (2)

ikegami
ikegami

Reputation: 385546

print "Result=", $a+$b, "\n";         # Multi-arg print
print "Result=".($a+$b)."\n";         # Concatenation
print "Result=${\( $a+$b )}\n";       # ref-deref trick (scalar context)
print "Result=@{[ $a+$b ]}\n";        # ref-deref trick (list context)
printf "Result=%s\n", $a+$b;          # printf
print sprintf("Result=%s\n", $a+$b);  # sprintf

Upvotes: 4

tripleee
tripleee

Reputation: 189317

Yes;

print "Result = ", $a+$b, "\n";

Upvotes: 4

Related Questions