为什么 Tcl 在调用“set"时不在变量名前使用美元符号? [英] Why doesn't Tcl use a dollar sign before variable names when calling "set"?

查看:23
本文介绍了为什么 Tcl 在调用“set"时不在变量名前使用美元符号?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将在这里使用 Perl 作为比较:

I am just going to use Perl as a comparison here:

$foo = 5;
print $foo;

将变量 $foo 设置为 5,然后打印变量的内容(注意 $foo 总是作为 $foo 访问>).

sets the variable $foo to 5, and then prints the contents of the variable (notice that $foo is always accessed as $foo).

在 Tcl 中:

set foo 5
puts $foo

做与 Perl 相同的事情.

does the same thing as the Perl counterpart.

为什么Tcl 不用$"设置变量,而是需要$"来访问变量?为什么对于过程也是如此(例如proc bar {spam egg} {...})?对我来说,Tcl 代码看起来是这样的(伪代码):

Why doesn't Tcl set variables with the "$", but need a "$" to access a variable? Why is this true for procedures too (e.g.proc bar {spam eggs} {...})? To me, the Tcl code looks like this (in pseudocode):

"foo" = 5 # Setting a string?
puts $foo # "$foo" is not defined.

(我的评论只反映了似乎正在发生的事情,而不是正在发生的事情).

(my comments only reflect what appears to be happening, not what is happening).

我想补充的另一点是这一点的清晰度:

Another point I want to add is the clarity of this:

set foo foo

是的,我总是可以做 set foo "foo",但是 set $foo foo 不是更一致吗?

Yeah, I could always do set foo "foo", but isn't set $foo foo more consistent?

据我所知,foo"可以是变量或字符串,具体取决于具体情况,如我上一个示例所示 (set foo foo = set var string),但我不明白这个语法(可能是因为我习惯了 Python...)

From what I know, "foo" can be a variable or a string, depending on the situation, as seen in my last example (set foo foo = set var string), but I don't get this syntax (maybe because I'm used to Python...)

推荐答案

我认为原来的 Tcl 只有 set 命令,所以获取变量foo"内容的唯一方法是调用 set foo.但是随着 Tcl 进入通用脚本语言领域(回想一下,Tcl 被设想为一种可嵌入语言,您可以在用 C 编写的复杂组件上使用薄薄的 Tcl 层,因此预计不会使用大量变量),认为$varname的语法糖有用,所以添加了.

I think the original Tcl only had the set command, so the only way to fetch the contents of a variable "foo" was calling set foo. But as Tcl progressed into the domain of general-purpose scripting languages (recall that Tcl was envisioned as being an embeddable language where you use a thin layer of Tcl over compilcated components written in C, so one wasn't expected to use lots of variables), it was deemed that that $varname syntactic sugar is useful and so it was added.

换句话说,Tcl 不像 Perl 那样使用 "$",其中 "$" 的意思是将后面的任何内容解释为标量",Tcl 中的 "$" 也不表示变量.相反,它只是一个语法糖,用于给我一个变量的值,该变量的名称由紧随其后的单词给出".

In other words, Tcl does not use "$" in the same way as Perl does, in which the "$" means "interpret whatever follows as a scalar", neither does "$" in Tcl denote a variable. Instead it merely a syntactic sugar for "give me the value of a variable whose name is given by the immediately following word".

这篇关于为什么 Tcl 在调用“set"时不在变量名前使用美元符号?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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