Reputation: 3235
This is derived from On tcl tests - how to interpret tcltest::test.
Here is from TCL8.4 man page:
[7] Variable substitution.
If a word contains a dollar-sign (“$
”) followed by one of the forms described below, then Tcl performs variable substitution: the dollar-sign and the following characters are replaced in the word by the value of a variable. Variable substitution may take any of the following forms:
$name
Name is the name of a scalar variable; the name is a sequence of one or more characters that are a letter, digit, underscore, or namespace separators (two or more colons).
So a single $
without characters like letter, digit, underscore or colon should be a syntax error.
Or it actually is a TCL parser bug but because there is no such real world script, so we get away with it?
Sorry if I seem crazily picky, just want to understand.
Upvotes: 0
Views: 52
Reputation: 113926
Carefully note the wording of the variable substitution rules:
If a word contains a dollar-sign ("$") followed by one of the forms described below
This specifically means that if a word contains a $
but is not followed by valid forms for variable names then it is just the literal character $
because no substitutions have occurred. Again note that I carefully bolded the word "substitution".
In tcl, the $
rule is not a rule for identifying variables like in Perl or PHP. It is a rule for replacing one string with another (think of it as a simplified s/var/value/
operator).
You can try out the following:
set foo hello
puts $foo // prints hello
puts hello$foo // prints hellohello (note the "contains" wording of the rule)
puts $ // prints $
puts hello$ // prints hello$
puts hello$hello // errors out about $hello not existing
So $
alone is not a syntax error.
What have changed is perhaps the expression parser. But for that you don't consult substitution rules, you need to read the man page for expr
(I've read both the 8.4 and 8.6 man pages and there's nothing about bareword strings so it's probably a bug in pre-8.6 versions of expr)
Upvotes: 1