gpt4 book ai didi

perl - 在此处的文档中插入常量的首选方法是什么?

转载 作者:行者123 更新时间:2023-12-04 02:39:53 25 4
gpt4 key购买 nike

我确定有几种方法可以将值“bar”插入到下面的 <> 中,但最干净的方法是什么,为什么?

use constant FOO => 'bar';

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

最佳答案

使用 Const::Fast而不是 Readonlyconstant .他们在没有任何扭曲的情况下进行插值。见 CPAN modules for defining constants :

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

...

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 :
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;

关于perl - 在此处的文档中插入常量的首选方法是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15793219/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com