Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

syntax

tcl

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 contents of the variable (notice that $foo is always accessed as $foo).

In Tcl:

set foo 5
puts $foo

does the same thing as the Perl counterpart.

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

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

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

like image 550
D K Avatar asked Sep 04 '11 12:09

D K


1 Answers

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

like image 167
kostix Avatar answered Sep 30 '22 17:09

kostix