perl 是否有与字符串中的 TCL expr 等效的函数
我有一个编写 html 页面的脚本。
很多内容是固定的,所以我用打印QQ!在单个语句中输出格式良好的文本。
其中几行需要嵌入一个变量 + 2,因此我最终预先计算了 $myVarplus2,在这种情况下这是可以的。
my $myVarPlus2 = $myVar + 2;
在 TCL 中,我可以在语句中使用 [expre $myVar + 2]。 Perl 有类似的概念吗?
这相当于我想做的事情,
print qq!
$('td:nth-child(n)').show();!
$('td:nth-child([expr $myVar + 2])').removeClass('failCount');!
我宁愿不分解该声明
print qq!
$('td:nth-child(n)').show();!
$('td:nth-child(" . $myVar + 2 . qq!').removeClass('failCount');!
,但接受它可能是最简单的方法。
I have a script that writes an html page.
Much of the content is fixed and so I use print qq! to output well formatted text in a single statement.
A few of the lines need a variable + 2 embedded in them, so I have ended up pre-calculating $myVarplus2, which is OK in this one case.
my $myVarPlus2 = $myVar + 2;
In TCL I could just use [expre $myVar + 2] within the statement. Is there a similar concept for Perl ?
This is the equivalent of what I would like to do
print qq!
$('td:nth-child(n)').show();!
$('td:nth-child([expr $myVar + 2])').removeClass('failCount');!
I would rather not break the statement up
print qq!
$('td:nth-child(n)').show();!
$('td:nth-child(" . $myVar + 2 . qq!').removeClass('failCount');!
but accept it may be the simplest approach.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
一种方法是使用 Perl 字符串中的穷人模板:
这取决于您自己的判断是否比分解语句更具可读性,以及在什么时候它变得难以维护。
另一种方法是 printf
但除了 Perl Golf 之外,除非这是一个简单的一次性项目,从长远来看,我认为您最好通过查看 模板工具包,然后你可以像这样编写你的输出
One way to do it, poor-man's templating in Perl strings:
It's up to your own judgement whether you find that more readable than breaking the statement up, and at what point it becomes difficult to maintain.
Another way to do it would be printf
But perl golf aside, unless this is a simple throwaway project, in the long run I think you'd be best served by looking at Template Toolkit, then you can write your output like this
您可以只插入一个包含单个表达式的匿名 arrayref:
但是,像这样混合 HTML 和 Perl 会很混乱。我建议使用模板模块,例如 HTML::Template 这将使您能够说:
等等,给定一个包含以下内容的模板:
You could just interpolate an anonymous arrayref containing the single expression:
However, mixing HTML and Perl like that is messy. I would recommend using a template module such as HTML::Template which would enable you to say:
etc given a template that contains:
有一种可怕的黑客方法可以通过创建一个包含表达式的匿名数组并立即引用该数组来做到这一点。但我真的不推荐它。
使用真正的模板系统要好得多。我推荐模板工具包。
There's a horrible hacky way to do this by creating an anonymous array containing your expression and deferencing the array immediately. But I really don't recommend it.
Far better to use a real templating system. I recommend the Template Toolkit.