perl 是否有与字符串中的 TCL expr 等效的函数

发布于 2024-12-15 14:24:59 字数 568 浏览 0 评论 0原文

我有一个编写 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

冧九 2024-12-22 14:24:59

一种方法是使用 Perl 字符串中的穷人模板:

print qq!
\$('td:nth-child(n)').show();
\$('td:nth-child(@{[ $myVar + 2 ]}).removeClass('failCount');!

这取决于您自己的判断是否比分解语句更具可读性,以及在什么时候它变得难以维护。

另一种方法是 printf

printf q!
$('td:nth-child(n)').show();
$('td:nth-child(%s).removeClass('failCount');!, $myVar + 2;

但除了 Perl Golf 之外,除非这是一个简单的一次性项目,从长远来看,我认为您最好通过查看 模板工具包,然后你可以像这样编写你的输出

$template = q!
$('td:nth-child(n)').show();
$('td:nth-child([% myVar +2 %]).removeClass('failCount');!;

One way to do it, poor-man's templating in Perl strings:

print qq!
\$('td:nth-child(n)').show();
\$('td:nth-child(@{[ $myVar + 2 ]}).removeClass('failCount');!

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

printf q!
$('td:nth-child(n)').show();
$('td:nth-child(%s).removeClass('failCount');!, $myVar + 2;

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

$template = q!
$('td:nth-child(n)').show();
$('td:nth-child([% myVar +2 %]).removeClass('failCount');!;
深巷少女 2024-12-22 14:24:59

您可以只插入一个包含单个表达式的匿名 arrayref:

print qq!
$('td:nth-child(n)').show();!
$('td:nth-child(@{[ $myVar + 2] })').removeClass('failCount');!

但是,像这样混合 HTML 和 Perl 会很混乱。我建议使用模板模块,例如 HTML::Template 这将使您能够说:

 $tmpl->param(
     MYVAR => $myVar,
     OTHERVAR => ($myVar + 2),
 );

等等,给定一个包含以下内容的模板:

$('td:nth-child(n)').show();
$('td:nth-child(<TMPL_VAR OTHERVAR>').removeClass('failCount');

You could just interpolate an anonymous arrayref containing the single expression:

print qq!
$('td:nth-child(n)').show();!
$('td:nth-child(@{[ $myVar + 2] })').removeClass('failCount');!

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:

 $tmpl->param(
     MYVAR => $myVar,
     OTHERVAR => ($myVar + 2),
 );

etc given a template that contains:

$('td:nth-child(n)').show();
$('td:nth-child(<TMPL_VAR OTHERVAR>').removeClass('failCount');
赠我空喜 2024-12-22 14:24:59

有一种可怕的黑客方法可以通过创建一个包含表达式的匿名数组并立即引用该数组来做到这一点。但我真的不推荐它。

#!/usr/bin/perl

use strict;
use warnings;
use 5.010;

my $my_var = 10;

my $output = "my_var is $my_var. my_var + 2 is @{[ $my_var + 2 ]}";

say $output

使用真正的模板系统要好得多。我推荐模板工具包

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.

#!/usr/bin/perl

use strict;
use warnings;
use 5.010;

my $my_var = 10;

my $output = "my_var is $my_var. my_var + 2 is @{[ $my_var + 2 ]}";

say $output

Far better to use a real templating system. I recommend the Template Toolkit.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文