我们需要“取消设置”吗? TCL的变量?

发布于 2024-08-23 01:55:48 字数 112 浏览 8 评论 0原文

是否需要良好的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

审判长 2024-08-30 01:55:48

可以在使用变量之前确定变量是否存在,使用 <代码>信息存在命令。确保如果您不使用 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 using unset, 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. Using unset 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.

暮光沉寂 2024-08-30 01:55:48

除了其他响应之外,如果您的 Tcl 版本足够新,您还可以使用:

unset -nocomplain foo

That'll unset foo if 存在,但如果不存在则不会抱怨。

In addition to the other responses, if your Tcl version is new enough, you can also use:

unset -nocomplain foo

That'll unset foo if it exists, but won't complain if it doesn't.

゛时过境迁 2024-08-30 01:55:48

根据系统统计数据,当脚本将大量数据存储到变量和数组中时,它可能会出现“无法分配字节”问题。一旦缓存或 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)

伪装你 2024-08-30 01:55:48

请注意,我似乎无法对上面的“信息存在”发表评论;

我经常使用这种形式..

if { [info exists pie] && [$pie == "ThisIsWhatIWantInPie"]} {
    puts "I found what I wanted in pie."
} else {
    puts "Pie did not exist; but I still did not error,TCL's evaluation \
          will see the conditional failed on the [info exists] and not \
          continue onto the comparison."
}

For note as I don't seem able to comment on the "info exists" above;

I use this form often..

if { [info exists pie] && [$pie == "ThisIsWhatIWantInPie"]} {
    puts "I found what I wanted in pie."
} else {
    puts "Pie did not exist; but I still did not error,TCL's evaluation \
          will see the conditional failed on the [info exists] and not \
          continue onto the comparison."
}
黯然#的苍凉 2024-08-30 01:55:48

除了其他响应之外,我想补充一点,如果您想忽略由于取消设置不存在的变量而引发的错误,请使用“catch”。

#!/bin/bash

catch {unset newVariable}

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'.

#!/bin/bash

catch {unset newVariable}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文