Skip to content
Tcl Source Code View Ticket
Login

View Ticket

commented if statement are evaluated

Closed / Invalid447296ffffffffffffffffffffffffffffffffffCreated 25 years agoUpdated 25 years ago
Descriptionplain2001-08-02 19:45:10
Code snippet:
if {<conditional1>} {
<action1>
#} elseif {<conditional2>}
#<action2>
} else {
<action3>
}

Problem:
If <conditional2> is true in above if statement,
<action3> will not execute. Nothing happens instead. It
appears that <conditional2> is being evaluated, but
<action2> is being ignored as commented code. I don't
know if this occurs with other conditional statements.

Expected:
Since <conditional2> is commented out, it should be
ignored and not evaluated.

OS:Solaris 5.8 for SPARC.

Tcl version:8.3.2
msoferplain2001-08-03 04:48:39
Logged In: YES 
user_id=148712

You are not actually commenting the ifelse clause. 

What is happening here is that the parser sees
  if expr1 body1 elseif expr2 body2 else body3
with scripts <body1> and <body2> defined as:
  * body1 -> \n <action1>  \n # 
  * body2 -> \n # <action2> \n

The '}' after the first '#' represents the end of the script
<body1>; commenting ends right there.

I'm hesitant to call this a "bug", as opposed to a
"mis-documented feature". The docs say  (rule [9]):
"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."

Maybe the docs should be more explicit and say instead:
" ..., up through the next newline or end-of-script
(whichever comes first), ..."
or something to that effect. However, this particular
description looks even more confusing to me.
msoferplain2001-08-03 05:23:57
Logged In: YES 
user_id=148712

The man page that describes Tcl's parsing is
  http://tcl.activestate.com/man/tcl8.4/TclCmd/Tcl.htm
Pay special attention to rule [5]

I think you will find the following wiki pages very helpful:
  http://mini.net/tcl/1012.html
  http://mini.net/tcl/1669.html
Log in (anonymous is fine) to add a comment.