[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

RE: Tcl comments and if statements




On Thu, 24 Aug 2000, Gregory W Cook wrote:

> > That's what you'd expect, but try the following:
> > 
> > -------------------
> > proc parse { } {
> > # if {1} {
> >   if {1} {
> >   label .b -text okay
> >   pack .b
> >   } else {
> >   label .b -text {not okay}
> >   pack .b
> >   }
> > }
> > 
> > parse
> > ----------------
> > 
> > The code should produce a parse error, when normally you wouldn't expect
> > that it would.  If you remove the open-brace in the comment, or adjust the
> > code so that it's no longer a procedure, then the error message disappears.
> > 
> > Brent Welsh's book has a warning about this tcl "feature" (page 17 in the
> > 3rd edition).
> > 
> 
> Thank you, that clears up the confusion.  Clearly, there is a difference
> between the tcl language -- "If the first nonblank character of a command
> is a #, the # and all characters following it up through the next
> newline are treated as a comment and disgarded.", Ousterhout, 1994, p. 37
> -- and the tcl implementation.

The O'Reilly book "TCL/TK In a Nutshell" devotes page and a half to this
topic: "Comments are treated as Commands!" In a nutshell, 

	"Comments look like those in shell-type languages, a line that
	begins with a '#'. However, TCL fully parses lines before deciding 
	that they should be executed (in the case of a command) or ignored 
        (in the case of a comment). You should think of a comment as a "do
	nothing" command rather than as a comment as in other languages. 
	Comments may appear where where TCL expects a command to appear."

In the example above, TCL detects the comment *only* after it has fully
parsed what it thinks it is a command -- the ending open brace makes it
keep scanning and parsing until it finds a matching brace or does not and
thus complains of a "missing close-brace."

-- Denis