start page | rating of books | rating of authors | reviews | copyrights

Perl Cookbook

Perl CookbookSearch this book
Previous: 1.9. Controlling Case Chapter 1
Strings
Next: 1.11. Indenting Here Documents
 

1.10. Interpolating Functions and Expressions Within Strings

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.

Solution

You can break up your expression into distinct concatenated pieces:

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

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

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

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.

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 will build a string with an interpolated expression and pass 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 we did 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.

In version 5.004 of Perl, ${\ EXPR } wrongly evaluates EXPR in list instead of scalar context. This bug is fixed in version 5.005.

See Also

perlref (1) and the "Other Tricks You Can Do with Hard References" section of Chapter 4 of Programming Perl


Previous: 1.9. Controlling Case Perl Cookbook Next: 1.11. Indenting Here Documents
1.9. Controlling Case Book Index 1.11. Indenting Here Documents

Library Navigation Links

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