Interpolating Functions and Expressions Within Strings (Perl Cookbook, 2nd Edition)

## 1.15. Interpolating Functions and Expressions Within Strings

### 1.15.1. Problem

You want a function call or expression to expand within a string. This lets you construct more complex templates than with simple scalar variable interpolation.

### 1.15.2. Solution

Break up your expression into distinct concatenated pieces:

>     $answer = $var1 . func( ) . $var2;   # scalar only

Or use the slightly sneaky `@{[` `LIST` `EXPR` `]}` or `${` `\(SCALAR` `EXPR` `)` `}` expansions:

>     $answer = "STRING @{[ LIST EXPR ]} MORE STRING";
>     $answer = "STRING ${\( SCALAR EXPR )} MORE STRING";

### 1.15.3. Discussion

This code shows both techniques. The first line shows concatenation; the second shows the expansion trick:

>     $phrase = "I have " . ($n + 1) . " guanacos.";
>     $phrase = "I have ${\($n + 1)} guanacos.";

The first technique builds the final string by concatenating smaller strings, avoiding interpolation but achieving the same end. Because `print` effectively concatenates its entire argument list, if we were going to `print` `$phrase`, we could have just said:

>     print "I have ",  $n + 1, " guanacos.\n";

When you absolutely must have interpolation, you need the punctuation-riddled interpolation from the Solution. Only `@`, `$`, and `\` are special within double quotes and most backquotes. (As with `m//` and `s///`, the `qx( )` synonym is not subject to double-quote expansion if its delimiter is single quotes! `$home` `=` `qx'echo` `home` `is` `$HOME';` would get the shell `$HOME` variable, not one in Perl.) So, the only way to force arbitrary expressions to expand is by expanding a `${ }` or `@{ }` whose block contains a reference.

In the example:

>     $phrase = "I have ${\( count_em( ) )} guanacos.";

the function call within the parentheses is not in scalar context; it is still in list context. The following overrules that:

>     $phrase = "I have ${\( scalar count_em( ) )} guanacos.";

You can do more than simply assign to a variable after interpolation. It's a general mechanism that can be used in any double-quoted string. For instance, this example builds a string with an interpolated expression and passes the result to a function:

>     some_func("What you want is @{[ split /:/, $rec ]} items");

You can interpolate into a here document, as by:

>     die "Couldn't send mail" unless send_mail(<<"EOTEXT", $target);
>     To: $naughty
>     From: Your Bank
>     Cc: @{ get_manager_list($naughty) }
>     Date: @{[ do { my $now = `date`; chomp $now; $now } ]} (today)

>     Dear $naughty,

>     Today, you bounced check number @{[ 500 + int rand(100) ]} to us.
>     Your account is now closed.

>     Sincerely,
>     the management
>     EOTEXT

Expanding backquotes (````) is particularly challenging because you would normally end up with spurious newlines. By creating a braced block following the `@` within the `@{[ ]}` anonymous array dereference, as in the last example, you can create private variables.

Although these techniques work, simply breaking your work up into several steps or storing everything in temporary variables is almost always clearer to the reader.

The Interpolation module from CPAN provides a more syntactically palatable covering. For example, to make elements of the hash `%E` evaluate and return its subscript:

>     use Interpolation E     => 'eval';
>     print "You bounced check number $E{500 + int rand(100)}\n";

Or to make a hash named `%money` call a suitably defined function of your choice:

>     use Interpolation money => \&currency_commify;

>     print "That will be $money{ 4 * $payment }, right now.\n";

expect to get something like:

>     That will be $3,232.421.04, right now.

### 1.15.4. See Also

perlref(1) and the "Other Tricks You Can Do with Hard References" section in Chapter 8 of Programming Perl; the Interpolation CPAN module

Copyright © 2003 O'Reilly & Associates. All rights reserved.