Why doesn't Tcl use the dollar sign in front of variable names when calling "set"?

I'm just going to use Perl for comparison here:

$foo = 5; print $foo; 

sets the variable $foo to 5, and then prints the contents of the variable (note that $foo always refers to $foo ).

In Tcl:

 set foo 5 puts $foo 

does the same thing as a perl copy.

Why doesn't Tcl set variables with "$", but does $ require access to the variable? Why is this also true for procedures (for example, proc bar {spam eggs} {...} )? For me, the Tcl code looks like this (in pseudo-code):

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

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

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

 set foo foo 

Yes, I could always set foo "foo" , but was not set $foo foo more consistent?

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

+6
source share
4 answers

I think the original Tcl only had the set command, so the only way to get the contents of the variable "foo" is to call set foo . But as Tcl moved into the field of general-purpose scripting languages ​​(recall that Tcl was introduced as an embedded language, where you use a thin layer of Tcl over compiled components written in C, so it was not expected to use many variables), it was considered that the syntax sugar $ varname is useful, and so it was added.

In other words, Tcl does not use "$" in the same way as Perl, in which "$" means "interpret everything that follows a scalar", and "$" in Tcl means a variable. Instead, it is just syntactic sugar for "give me the value of a variable whose name is given immediately with the next word."

+11
source

Well, I decided to comment on the last fragments of the original question, but the comment has outgrown the limit, so I will send it as an answer, even if it does not affect the specified question. I hope this clarifies some of the things that seem to be implied in the question.

Consistency note set foo foo : you are approaching Tcl in a slightly incorrect way. Tcl is radically different from Perl or Python in that it has almost no syntax (indeed, like LISP). "set foo foo" is not syntax for setting a variable, since it can be in another language, it is a call to the command currently available under the name "set" and passing two arguments - "foo" and "foo"; regardless of what is registered under the name "set", it decides what the variable name is and what the value is for setting it. Similarly, loopback commands are not syntactic; they are commands. Indeed, it is worth stopping here and thinking that

 for {set i 0} {$i < 10} {incr i} { puts $i } 

- it’s not just the idea of ​​a fancy language designer to use curly braces or β€œregular” parentheses instead, for example, in C. Instead, the Tcl parser parses five words, accepts the first name of the command name, looks like this and passes the rest of the words; this is a for implementation that interprets those {set i 0} , {$i < 0} , etc. and performs them accordingly. The syntax is only to determine how words are parsed from the input character stream. And since everything is implemented using commands, your commands may not differ from the built-in ones; this allows you to create custom commands as powerful as for or switch (something similar to macros in LISP). In another language, this would be equal to the ability to extend sytnax in arbitrary ways.

When this idea "clicks", Tcl will no longer seem strange. And this is the property that makes Tcl so easily extensible (and embeddable): because the commands that you provide for your part behave in exactly the same way as all built-in commands.

+10
source

The real answer, if any, can only be provided by the original language developers (John Ousterhout), I think. Thus, the rest is open to thought or (educated) guesswork. There is some story about TCL available here , but with a quick read there is no direct answer.

Why doesn't Tcl set variables with "$", but do I need "$" to access the variable?

My trick would be to get it closer to the shell languages ​​(UNIX). TCL was conceived in Berkeley, perhaps in a strong UNIX environment (or BSD for that matter).

UNIX shells also do not use the $ sign (or the equivalent for the corresponding shell) when declaring or assigning variables, but they require it when referenced:

 # bourn shell like shells (sh, bash, ksh, ...) foo=foo echo "The value of the variable foo is $foo." 

Even the wicked Windows processor CMD.EXE uses a comparable method (although, I think, this is not what TCL designers thought about;)

 REM DOS COMMAND.COM / Windows CMD.EXE set foo=foo echo The value of the variable is %foo%. 

In addition, "string values" (although shells are known to be weakly typed) usually do not require quotation marks if there are spaces in the string values.

Yes, I could always set foo "foo", but not set $ foo foo more sequential?

Well, this is not for TCL designers / creators; -)

EDIT I almost forgot: in TCL you could do the following:

 set foo bar set $foo something puts $bar 

It really outputs "something." The second line actually sets the string "something" to the value of the variable "foo", thereby setting the variable named "bar" and assigning it the value "something".

+2
source

Think of $foo as the value of the variable foo , that is, the text that was set last. Now, if foo currently has the value x , set $foo y means set xy , which does not explicitly mean what is meant. It is actually more consistent than other languages, where foo sometimes means the value of the variable foo , and sometimes the variable foo itself.

+1
source

Source: https://habr.com/ru/post/896586/


All Articles