我们需要“取消设置”吗? TCL的变量?
是否需要良好的 TCL 代码?如果我们在脚本中不使用“unset”关键字会发生什么?有什么我应该知道的不良影响吗?
我继承了一些遗留代码,并且由于“未设置”不存在的变量而产生的错误让我陷入困境!
Is it a requirement of good TCL code? What would happen if we don't use the "unset" keyword in a script? Any ill-effects I should know about?
I'm inheriting some legacy code and the errors that come about due to "unset"-ing non-existent variables are driving me up the wall!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
可以在使用变量之前确定变量是否存在,使用 <代码>信息存在命令。确保如果您不使用
unset
,则不会扰乱其他地方的程序逻辑。没有 Tcl 特定的原因来
取消设置
变量,也就是说,它不会导致内存泄漏或耗尽变量句柄或类似的疯狂情况。使用unset
可能是一种防御性编程实践,因为它可以防止将来在不再相关的变量后使用该变量。如果不了解您正在使用的确切代码,则很难提供更详细的信息。It's possible to determine whether a variable exists before using it, using the
info exists
command. Be sure that if you're not usingunset
, that you don't upset the logic of the program somewhere else.There's no Tcl-specific reason to
unset
a variable, that is, it's not going to cause a memory leak or run out of variable handles or anything crazy like that. Usingunset
may be a defensive programming practice, because it prevents future use of a variable after it's no longer relevant. Without knowing more about the exact code you're working with, it's hard to give more detailed info.除了其他响应之外,如果您的 Tcl 版本足够新,您还可以使用:
That'll unset foo if 存在,但如果不存在则不会抱怨。
In addition to the other responses, if your Tcl version is new enough, you can also use:
That'll unset foo if it exists, but won't complain if it doesn't.
根据系统统计数据,当脚本将大量数据存储到变量和数组中时,它可能会出现“无法分配字节”问题。一旦缓存或 RAM 已满,它就会中断,并显示“无法分配 XXXXXXXX 字节”。
确保您没有将那么多数据存储到变量中,否则一旦各个数据集(变量)的使用结束,请取消设置
Depends on the system stats it may give "unable to allocate bytes" issue as and when your script is storing huge data into variables and arrays. it'll break once the cache or RAM is full saying "unable to allocate XXXXXXXX bytes".
Make sure you're not storing that much data into variables, otherwise do unset once the use is over for the respective datasets(variables)
请注意,我似乎无法对上面的“信息存在”发表评论;
我经常使用这种形式..
For note as I don't seem able to comment on the "info exists" above;
I use this form often..
除了其他响应之外,我想补充一点,如果您想忽略由于取消设置不存在的变量而引发的错误,请使用“catch”。
In addition to the other responses, I want to add that, if you want to neglect the errors raising as a result of unsetting non-existent variable use 'catch'.