对于一般的文本构建(关于您的更新),使用模板引擎是一个异乎寻常的答案。它有点像类似于 printf 的强化版!
以下是使用 Template Toolkit
的示例:
sub buildtt {
use Template;
my $tt = Template->new( START_TAG => '{', END_TAG => '}' );
$tt->process( \$_[0], $_[1], \my $output );
return $output;
}
my $str = buildtt '{a} {b} {c}' => {
a => "123 123 456 sdndfnd sdfdmd",
b => "dfsdjkfs 343489 dfjsdj 3 34kdfsk kd",
c => "fd kd9534 rfg 546 5",
};
你也可以这样构建:
my $str2 = buildtt '{all.join(" ")}' => {
all => [ "123 123 456 sdndfnd sdfdmd",
"dfsdjkfs 343489 dfjsdj 3 34kdfsk kd",
"fd kd9534 rfg 546 5" ],
};
这里有一个带引用等的示例:
my $str3 = buildtt '{all.join(" ")}' => {
all => [ "no quoted text here",
"here's some and here's some more",
q{$str2 was "buildtt"},
$str2 ],
};
一个更好的例子是使用你原始的SQL文本,像这样:
my $sql = buildtt 'select {v.join(",")} from {t} where {q}' => {
v => [qw/ query_accession query_tag hit_accession hit_tag significance /],
t => 'summaryTables',
q => '( query_id = ? )',
};
另请参阅:
最后,您甚至可以使用类似引用的运算符和PerlX::QuoteOperator创建新的奇异操作:
use PerlX::QuoteOperator q_strip_newline => {
-emulate => 'q',
-with => sub ($) {
my $txt = shift;
$txt =~ s/\n//g;
$txt;
},
};
my $str = q_strip_newline{123 123 456 sdndfnd sdfdmd
dfsdjkfs 343489 dfjsdj 3 34kdfsk kd
fd kd9534 rfg 546 5};
/I3az/