Why doesn't Tcl use a dollar sign before variable names when calling “set”?

前端 未结 4 1723
悲哀的现实
悲哀的现实 2020-12-04 01:52

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

$foo = 5;
print $foo;

sets the variable $foo to 5, and then prints the conte

4条回答
  •  萌比男神i
    2020-12-04 02:11

    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.

    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".

提交回复
热议问题