在此处的文档中插入常量的首选方法是什么? [英] What is the preferred way of interpolating a constant in a here doc?

查看:39
本文介绍了在此处的文档中插入常量的首选方法是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我确信有几种方法可以在下面的 <> 中插入值 'bar',但最简洁的方法是什么,为什么?

I'm sure there are several ways of getting the value 'bar' to interpolate in the <> below, but what is the cleanest way, and why?

use constant FOO => 'bar';

my $msg = <<EOF;
Foo is currently <whatever goes here to expand FOO>
EOF

推荐答案

使用 Const::快速 而不是Readonlyconstant.他们在没有任何扭曲的情况下进行插值.请参阅用于定义常量的 CPAN 模块:

Use Const::Fast instead of Readonly or constant. They interpolate without any contortions. See CPAN modules for defining constants:

对于条件编译,constant 是一个不错的选择.这是一个成熟的模块,应用广泛.

For conditional compilation, constant is a good choice. It's a mature module and widely used.

...

如果你想要数组或哈希常量,或者不可变的丰富数据结构,请使用 Const::Fast.它与 Attribute::Constant 之间的竞争很激烈,但 Const::Fast 似乎更成熟,并且发布了更多版本.

If you want array or hash constants, or immutable rich data structures, use Const::Fast. It's a close race between that and Attribute::Constant, but Const::Fast seems maturer, and has had more releases.

另一方面,您似乎正在编写自己的模板代码.别.相反,使用一些简单的东西,比如 HTML::Template:

On the other hand, you seem to be writing your own templating code. Don't. Instead, use something simple like HTML::Template:

use HTML::Template;

use constant FOO => 'bar';

my $tmpl = HTML::Template->new(scalarref => \ <<EOF
Foo is currently <TMPL_VAR VALUE>
EOF
);

$tmpl->param(VALUE => FOO);
print $tmpl->output;

这篇关于在此处的文档中插入常量的首选方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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