Item 58: Use @{[ ]} or a tied hash to evaluate expressions inside strings.Double-quote interpolation works just fine for variables , slices, and the like:
It even works for reference syntax and objects:
However, double-quote interpolation does not work for subroutine calls and other types of expressions. For example, after seeing the above work, you might try:
Here, only the scalar variable part, $golfer , of $golfer->name is interpolated into the double-quoted string, and the result is probably not what was desired. You can work around this with a strange bit of Perl syntax. Just combine the anonymous array constructor [ ] with the dereferencing syntax for arrays: Continued from above:
It's U.B.E.: Ugly But Effective. Even if the syntax is ugly, this construct can be very helpful when you are trying to put together a long here doc string, but discover that you need to interpolate some sort of expression in the middle of it: $a = 2; $b = 3; print <<EOT; Here are the answers: $a + $b is @{[$a + $b]} $a * $b is @{[$a * $b]} EOT This isn't a particularly inspired example, but if you've ever used a here doc string to generate HTML in CGI script, you've probably come across a more compelling situation. There is another alternative, which may or may not seem simpler to you. It does, however, eliminate some more punctuation. You can use a tied hash (see Item 52): sub Print::TIEHASH { bless \ my $thingy, shift() } sub Print::FETCH { $_[1] } tie %print, Print; $a = 2; $b = 3; print <<EOT; Here are the answers: $a + $b is $print{$a + $b} $a * $b is $print{$a * $b} EOT Note that in this case the inside of $print{} is a scalar context. You also can use a list inside @print{} , in which case the output will be separated by spaces, as usual. So what happens if you have a list to print, but you don't want spaces between the items? One possibility is changing the value of the $" special variable. I don't recommend this, but if you decide to go this route anyway, be sure to localize the damage, er, change:
If you don't mind a more complicated syntax, you can return to the tied variable. Let's modify the example so that we can specify a "glue" string. We have to use a reference to an array as a subscript to make this work, so we're back to the anonymous array constructor again:
|