在 Perl 中我应该使用什么来代替 printf ? [英] What should I use instead of printf in Perl?

查看:71
本文介绍了在 Perl 中我应该使用什么来代替 printf ?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在 Perl 中使用一些字符串替换来简化翻译,即替换很多

I need to use some string replacement in Perl to ease translations, i.e. replace many

print "Outputting " . $n . " numbers";

类似的东西

printf ("Outputting %d numbers", $n);

但是,我想用更容易为人类解析的东西替换 printf,如下所示:

However, I'd like to replace printf with something easier to parse for humans, like this:

printX ("Outputting {num} numbers", { num => $n });

或者更一般的东西.

你能推荐一些你喜欢和使用的东西(来自或不来自 CPAN)吗?

Can you recommend something (from CPAN or not) you like and use?

推荐答案

CPAN 上的大多数模板模块可能都能满足您的需求.这是一个使用 模板工具包...

Most Templating modules on CPAN will probably do what you want. Here's an example using Template Toolkit...

use Template;
my $tt = Template->new;

$tt->process( \"Outputting [% num %] numbers\n", { num => 100 } );


你可以用这样的东西模仿你需要的例子......


And you can mimic your required example with something like this...

sub printX {
    use Template;
    my $tt = Template->new( START_TAG => '{', END_TAG => '}' );
    $tt->process( \( $_[0] . "\n" ), $_[1] );
}

你有...

printX 'Outputting {num} numbers' => { num => 100 };

这篇关于在 Perl 中我应该使用什么来代替 printf ?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆