在TCL中使用变量

发布于 2025-02-07 19:09:50 字数 565 浏览 3 评论 0原文

我正在尝试在TCL中的其他Proc中使用全局变量。

这是一个简短的例子:

proc myname {} {
    set ::name [gets stdin]
}

proc myname2 {} {
    puts "your name is: $name"
}
tcl_shell> proc myname

Jhon <--- "Jhon" should be stored in varible called *name*

tcl_shell> proc myname2

your name is Jhon <-- I want something like this.

但是我仍然看到此错误:错误:无法读取“名称”:没有这样的变量,

我也尝试过:

proc myname {} {
    global name
    set name [gets stdin]
}
    
proc myname2 {} {
    puts "your name is: $name"
}

I'm trying to use a global variable in other proc in tcl.

Here's a short example:

proc myname {} {
    set ::name [gets stdin]
}

proc myname2 {} {
    puts "your name is: $name"
}
tcl_shell> proc myname

Jhon <--- "Jhon" should be stored in varible called *name*

tcl_shell> proc myname2

your name is Jhon <-- I want something like this.

But I'm still seeing this error: Error: can't read "name": no such variable

I also have tried this:

proc myname {} {
    global name
    set name [gets stdin]
}
    
proc myname2 {} {
    puts "your name is: $name"
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

汐鸠 2025-02-14 19:09:50

您正在尝试读取myname2范围中的变量名称

proc myname2 {} {
  puts "your name is: $name"
}

:当然,myname2 < /代码>功能。您保存的变量在全局(::)范围中,因此您可以执行:

proc myname2 {} {
  global name
  puts "your name is: $name"
}

proc myname2 {} {
  puts "your name is: $::name"
}

与C/C ++ TCL不同,默认情况下会隐藏全局范围。您需要故意将变量从全局范围导入到您的功能中。

You are trying to read a variable name that is in the myname2 scope:

proc myname2 {} {
  puts "your name is: $name"
}

Of course there is no variable name inside the myname2 function. The variable you saved to is in global (::) scope so you can either do:

proc myname2 {} {
  global name
  puts "your name is: $name"
}

or

proc myname2 {} {
  puts "your name is: $::name"
}

Unlike C/C++ tcl hides the global scope by default. You need to deliberately import variables from global scope into your functions.

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