Version 4 of comment

Updated 2002-09-09 15:18:17

If a hash character ("#") appears at a point where Tcl is expecting the first character of the first word of a command, then the hash character and the characters that follow it, up through the next newline, are treated as a comment and ignored. The comment character only has significance when it appears at the beginning of a command. (From: TclHelp).


Tcl students frequently ask "Why can I not place unmatched braces in Tcl comments?"


Tcl comments are almost like null-operation commands. You could write one yourself like this:

 proc -- args {}
 -- This is a call to the new proc that does nothing, like a comment
 -- but this here still does an [exit]!! So it's not like
 # [exit] [exit] [exit] ... and still won't die ;-)

Note the "if 0 ..." idiom:

 if 0 {
 Any Tcl code to be commented out (with matching braces, of course!)
 or any other kind of text, will be ignored - and even [exit] won't fire
 because it's in braces, so left unevaluated!

} and a fancy sugar for that: "#" is special only if first character of a command name. Nobody hinders you to write a

 proc {#} args {}

where the braces are only visual markup - the parser strips them off, the proc's name will be just "#". But to call it, you have to escape the "#" sign - with a backslash, or with my favorite sugar:

 {#} {
   This is a comment in braces
 }

where the name of the command is pretty self-documenting: "comment in braces"... Much more intuitive than e.g. Ruby's "=begin ... =end", and available with almost zero implementation! (RS)


Tcl syntax help - Arts and crafts of Tcl-Tk programming