Tcl/Tk的Switch模块中慎用注释

本文记录了解决Tcl中switch语句内错误放置注释导致的问题。通过调整注释位置,避免将其解释为模式的一部分,成功修复了代码。

今天改了一段Tcl代码,一段非常复杂的非常长的swith代码,总是遇到这样一个奇怪的错误:

"extra switch pattern with no body, this may be due to a comment incorrectly placed outside of a switch body - see the "switch" documentation"
于是我检查{}是否匹配,检查entry写的是否正确,检查switch的变量是否正确……反复几遍之后,错误仍然在,几近崩溃。
Google了半天,也没找到解决方法。
最终还是抱着试试看的心态打开了Tcl/Tk经典书《Practical Programming in Tcl and Tk》,打开第六章第二节,发现本节结束处赫然写着这样一段话:

A comment can occur only where the Tcl parser expects a command to begin. This restricts the location of comments in a switch command.

You must put them inside the command body associated with a pattern, as shown in Example 6-6. If you put a comment at the same level as

the patterns, the switch command will try to interpret the comment as one or more pattern-body pairs.

该示例如下:
Example 6-6 Comments in switch commands
switch -- $value {
   # this comment confuses switch
   pattern { # this comment is ok }
}
 
我赶紧查看了一下我的代码,发现有好几个处“} ;#end of XXX”这样的注释。把它们去掉之后,就可以正常work了。
下面就是带有错误注释的不能工作的代码段。
                                         append C(HostMMCAP) ",$ap"
                                                # Record this reservd AP
                                                append MMCAPRSVD " $ap"
				        } ;# end of if
                                } else {
                                        AlertBox warn /
"You can NOT reserve it! This AP has been reserved by 
$rsv_login in $rsv_site with COOL session (COOLID) $rsv_id.

Please contact user /"$rsv_login/" if you really want to use it."
                                        set MMCAPDISPLAY($ap) 0
                                        return
                                } ;# end of else
                        } ;# end of entry "reserved"
 
由此得知,虽然“;#”这样的注释在Tcl中一般是很常见的,但是在switch中语句中是不能用的,只能在单独一行里写注释。
以此文为记。

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值