我是否总是必须向 Tkx 的 -command 参数提供匿名子例程?

发布于 2024-08-28 21:39:29 字数 516 浏览 9 评论 0原文

我觉得有点奇怪,在为 Tkx 小部件指定 -command 参数时,我必须匿名包装定义的子例程。

TkDocs 教程 的摘录演示了这一点:

my $cb = $frm->new_ttk__button ( -text => "Calculate",
                                 -command => sub {calculate();}  );

sub calculate {
   $meters = int(0.3048*$feet*10000.0+.5)/10000.0 || '';
}

为什么我写的时候它不起作用<代码>-命令=> &calculate() 或 -command => \&计算()

I find it a bit weird that I have to wrap defined subroutines anonymously when specifying the -command argument for Tkx widgets.

An excerpt from a TkDocs tutorial demonstrates this:

my $cb = $frm->new_ttk__button ( -text => "Calculate",
                                 -command => sub {calculate();}  );

sub calculate {
   $meters = int(0.3048*$feet*10000.0+.5)/10000.0 || '';
}

Why doesn't it work when I write -command => &calculate() or -command => \&calculate()?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

著墨染雨君画夕 2024-09-04 21:39:29

你的语法不太正确。您的示例调用子例程(& 绕过任何原型)并传递返回值 (&calculate()) 或对其的引用 (\ &calculate()) 到 -command 选项。您想要分配对子例程本身的引用,可以通过以下方式完成:

-command => \&calculate

注意缺少尾随括号。另请注意,您不能以这种方式传递参数。如果你想这样做,你需要将调用包装在匿名子例程中:

-command => sub { calculate(12) }

或者向选项传递 ARRAY 引用而不是 CODE 引用:

-command => [\&calculate, 12]

这两种形式之间存在细微差别,如果您使用变量而不是文字,这一点很重要价值。

-command => sub { calculate($x) }  # creates a closure over $x
-command => [\&calculate, $x]      # makes a copy of $x

使用第一种形式对 $x 的更改将在调用命令时可见。在第二种形式下,他们不会;每次调用都会看到创建绑定时的值。两种形式都很有用;您只需要在决定使用哪个时做出良好的判断即可。

You don't have the syntax quite right. Your examples call the subroutine (the & bypasses any prototypes) and passes either the return value (&calculate()) or a reference to it (\&calculate()) to the -command option. You want to assign a reference to the subroutine itself, which you can do via:

-command => \&calculate

Note the lack of trailing parentheses. Also note that you can't pass arguments this way. If you want to do that you need to either wrap the call in an anonymous subroutine:

-command => sub { calculate(12) }

or pass the option an ARRAY reference instead of a CODE reference:

-command => [\&calculate, 12]

There's a subtle difference between the two forms that's important if you use a variable instead of a literal value.

-command => sub { calculate($x) }  # creates a closure over $x
-command => [\&calculate, $x]      # makes a copy of $x

Using the first form changes to $x will be visible when the command is invoked. Under the second form they won't be; each invocation will see the value at the moment the binding was created. Both forms are useful; you just need to exercise good judgment when deciding which to use.

太阳男子 2024-09-04 21:39:29

它应该与 -command => 一起使用\&计算。请注意缺少括号。

您给出的示例实际上是执行子例程并将其返回的值分配给 -command 键。

有关详细信息,请参阅 perlsubperlref

It should work with -command => \&calculate. Note the lack of parentheses.

The examples you gave are actually executing the subroutine and assigning the value returned by it to the -command key.

For more info see perlsub and perlref

深空失忆 2024-09-04 21:39:29

扎伊德说:“我很开明。但我
仍然不明白为什么 \&calculate
语法对我不起作用...... – Zaid
24 分钟前"

Zaid,\&calculate 语法确实有效。您试图使用 \&calculate() 语法。第一个绑定到“对稍后运行的子例程计算的引用”,后者绑定为“对(不带参数的调用计算的返回值,并绕过参数原型)的引用。

您是否有更长的、可编译的代码片段可供我们用于测试/分析?我最近对TK不太熟悉。

Zaid said, "I'm enlightened. But I
still don't see why the \&calculate
syntax doesn't work with me... – Zaid
24 mins ago"

Zaid, the \&calculate syntax does work. You were trying to use the \&calculate() syntax. The first binds to "a reference to the subroutine calculate to be run later", the latter binds as "a reference to (the return value of a call calculate with no arguments, and bypassing argument prototypes ).

Do you have a longer, compilable code snippet that we can use for testing/analysis? I am not recently familiar with TK.

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