Reputation: 357
Look at the following code sample.
$ct=5;
print "hey $ct+1";
When evaluated, this returns:
hey 5+1
However, I'm trying to get the code to return:
hey 6
Does anybody know if there is some sort of evaluation call that I can make to have this occur correctly, or a way to change the syntax a tad? I know I could simply do:
$dum=$ct+1;
print "hey $dum";
But what I'm showing you is a simple version of something more complicated. If I could get this small example to work correctly, my larger problem is solved. Thanks.
Upvotes: 3
Views: 13019
Reputation: 33360
Another alternative is formatted printing.
my $ct = 5;
printf "hey %s", $ct+1;
Upvotes: 8
Reputation: 118595
This is kind of intermediate Perl, but you can put your expression into a reference and dereference it inside double quotes:
print "hey @{[$ct+1]}";
print "hey ${\($ct+1)}";
Code like this is harder to read and gives Perl the reputation of being unfriendly to newbies, so I don't recommend it over extracting the expressions out of the quotes:
print "hey ", ($ct+1);
print "hey " . ($ct+1);
Upvotes: 2
Reputation: 385546
You want to execute a Perl addition operator, but the code only features a Perl string literal. You want
print "hey ", $ct+1;
If you did have Perl source code in a variable and you wanted to execute it, you'd have to invoke the Perl parser and compiler: eval EXPR
.
Upvotes: 9