Found in /usr/share/perl/5.34/pod/perlfaq4.pod
How do I expand function calls in a string?
(contributed by brian d foy)
This is documented in perlref, and although it's not the easiest thing
to read, it does work. In each of these examples, we call the function
inside the braces used to dereference a reference. If we have more than
one return value, we can construct and dereference an anonymous array.
In this case, we call the function in list context.
print "The time values are @{ [localtime] }.\n";
If we want to call the function in scalar context, we have to do a bit
more work. We can really have any code we like inside the braces, so we
simply have to end with the scalar reference, although how you do that
is up to you, and you can use code inside the braces. Note that the use
of parens creates a list context, so we need "scalar" to force the
scalar context on the function:
print "The time is ${\(scalar localtime)}.\n"
print "The time is ${ my $x = localtime; \$x }.\n";
If your function already returns a reference, you don't need to create
the reference yourself.
sub timestamp { my $t = localtime; \$t }
print "The time is ${ timestamp() }.\n";
The "Interpolation" module can also do a lot of magic for you. You can
specify a variable name, in this case "E", to set up a tied hash that
does the interpolation for you. It has several other methods to do this
as well.
use Interpolation E => 'eval';
print "The time values are $E{localtime()}.\n";
In most cases, it is probably easier to simply use string concatenation,
which also forces scalar context.
print "The time is " . localtime() . ".\n";
How do I expand tabs in a string?
You can do it yourself:
1 while $string =~ s/\t+/' ' x (length($&) * 8 - length($`) % 8)/e;
Or you can just use the Text::Tabs module (part of the standard Perl
distribution).
use Text::Tabs;
my @expanded_lines = expand(@lines_with_tabs);
How can I expand variables in text strings?
(contributed by brian d foy)
If you can avoid it, don't, or if you can use a templating system, such
as Text::Template or Template Toolkit, do that instead. You might even
be able to get the job done with "sprintf" or "printf":
my $string = sprintf 'Say hello to %s and %s', $foo, $bar;
However, for the one-off simple case where I don't want to pull out a
full templating system, I'll use a string that has two Perl scalar
variables in it. In this example, I want to expand $foo and $bar to
their variable's values:
my $foo = 'Fred';
my $bar = 'Barney';
$string = 'Say hello to $foo and $bar';
One way I can do this involves the substitution operator and a double
"/e" flag. The first "/e" evaluates $1 on the replacement side and turns
it into $foo. The second /e starts with $foo and replaces it with its
value. $foo, then, turns into 'Fred', and that's finally what's left in
the string:
$string =~ s/(\$\w+)/$1/eeg; # 'Say hello to Fred and Barney'
The "/e" will also silently ignore violations of strict, replacing
undefined variable names with the empty string. Since I'm using the "/e"
flag (twice even!), I have all of the same security problems I have with
"eval" in its string form. If there's something odd in $foo, perhaps
something like "@{[ system "rm -rf /" ]}", then I could get myself in
trouble.
To get around the security problem, I could also pull the values from a
hash instead of evaluating variable names. Using a single "/e", I can
check the hash to ensure the value exists, and if it doesn't, I can
replace the missing value with a marker, in this case "???" to signal
that I missed something:
my $string = 'This has $foo and $bar';
my %Replacements = (
foo => 'Fred',
);
# $string =~ s/\$(\w+)/$Replacements{$1}/g;
$string =~ s/\$(\w+)/
exists $Replacements{$1} ? $Replacements{$1} : '???'
/eg;
print $string;
Generated by phpMan Author: Che Dong On Apache Under GNU General Public License - MarkDown Format
2026-05-23 05:59 @216.73.217.24 CrawledBy Mozilla/5.0 AppleWebKit/537.36 (KHTML, like Gecko; compatible; ClaudeBot/1.0; +claudebot@anthropic.com)