The dereferenced arrayref technique works fine: "a string @{[foo()]}\n". But it's a little magical; it requires some tribal knowledge or testing for a reader of the code to understand what is happening.
Perl also provides printf, which is well documented and should be clearer for a reader:
sub foo { return "bar" }
printf "foo returns %s\n", foo();
Additionally, there are a lot of template systems available to Perl. Template::Toolkit, Text::Template, Mojo::Template, Template::Tiny, and many others. Here is an example using Template::Toolkit:
#!/usr/bin/env perl
use strict;
use warnings;
use Template;
sub foo {return "bar"}
my $output;
Template
->new
->process(
\"foo returns [% f %]\n",
{f => foo()},
\$output,
);
print $output;
This code works as-is with Template::Tiny if you just replace any mention of Template with Template::Tiny.
|