TCL/TK的隐藏特点

发布于 2024-07-25 16:33:06 字数 115 浏览 7 评论 0原文

我一直在使用 TCL/TK,最近开始在我的自动化应用程序中使用 TCL/TK,我渴望知识。

继续一长串隐藏功能问题,我想知道 TCL/TK 的任何隐藏或方便的功能,或者任何实现一些大操作的简单方法

I've been working with TCL/TK ,recently started to use TCL/TK with my automation applications and I'm hungry for knowledge.

To continue with the long line of Hidden Feature questions, I would like to know any hidden or handy features of TCL/TK or any easy method to achieve some big operations

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

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

发布评论

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

评论(11

私藏温柔 2024-08-01 16:33:07

所有 Tcl 的“关键字”都是常规 Tcl 命令,包括 [for]、[foreach]、[while] 等控制结构。这意味着您可以通过用纯 Tcl 代码编写新的控制结构来扩展语言。

例如,已实现 try/on/trap 结构在 Tcl 8.6a 中仅使用 Tcl 代码。 类似地,tcllib 包含 control::do,一个 do/while 控制结构。

其中很多都是通过 [upvar] 和 [uplevel] 命令实现的,这些命令允许您访问变量或在不同的堆栈帧中执行代码。

All of Tcl's "keywords" are regular Tcl commands, including control structures like [for], [foreach], [while], etc. This means that you can extend the language by writing new control structures in pure Tcl code.

For example, the try/on/trap structure has been implemented in Tcl 8.6a using only Tcl code. Similarly tcllib contains control::do, a do/while control structure.

A lot of this is made possible through the [upvar] and [uplevel] commands, which allow you to access variables or execute code in a different stack frame.

倚栏听风 2024-08-01 16:33:07

Tcl 是一种简单、开放的语言,几乎没有隐藏的功能。 这一切都是公开的,供程序员扩展和适应。

Tcl is such a simple, open language there are very few hidden features. It's all exposed for the programmer to extend and adapt.

可爱暴击 2024-08-01 16:33:07

恕我直言,Tcl 最大的隐藏功能是它的 C API。 使用它,可以很容易地包装核心 C程序或子系统并用 Tcl 编写 GUI 或其他功能。 虽然此功能并非 Tcl 所独有,但 Tcl 的设计目的是从头开始执行此操作,并且 C API 特别易于使用。

第二大隐藏功能是打包器,它是所有几何管理器的鼻祖。 有了这个,GUI 就可以拥有相当大的窗口,而代码量却少得惊人。 值得注意的是,Tcl/Tk 至少在 .net 出现之前 10 年就已经有了几何管理。

Tcl 的第三大特性是能够通过 C API 或使用 Tcl 中定义的命令来扩展语言。 不完全是 LISP 宏,但仍然非常灵活。 Expect 是一个非常好的应用程序示例,它围绕扩展基础 Tcl 语言以创建特定于域的脚本语言而构建。

编辑:好吧,烦我,Xt 确实有一个几何管理器,尽管我同意 Nat 的观点,因为它比 pack 更痛苦;-}

IMHO the greatest hidden feature of Tcl is its C API. Using this, it's really easy to wrap a core C program or subsystem and write a GUI or other functionality in Tcl. While this feature is not unique to Tcl, Tcl was designed to do this from the ground up and the C API is particularly easy to work with.

The second greatest hidden feature is the packer, the grand-daddy of all geometry managers. With this, a GUI can have sizeable windows with a surprisingly small amount of code. It's important to note that Tcl/Tk had geometry management at least 10 years before .net came out.

The third greatest feature of Tcl is the ability to exend the language, either through the C API or with commands defined in Tcl. Not quite LISP macros, but quite flexible nonetheless. Expect is a very good example of an application built around extending the basse Tcl language to make a domain-specific scripting language.

EDIT: well, bugger me, Xt really did have a geometry manager, although I agree with Nat in that it's somewhat more painful than pack ;-}

情绪失控 2024-08-01 16:33:07
  1. [数组名称] 也是新手询问如何迭代数组的第一个问题之一
  2. ,事实上,您可以 foreach {key1 key2} {$list1 $list2} {.. .} - 即使列表大小不同,
  3. 您也不应该在 switch case 之间使用注释(这不是一个很酷的功能,但大多数开发人员不理解
  4. rename 命令可以重命名任何函数/过程
  1. [array names] is one of the first questions newbies ask about how to iterate over an array
  2. also, the fact that you can foreach {key1 key2} {$list1 $list2} {...} - even if the lists are of different size
  3. you should not use comments between switch cases (this is not a cool feature but a problem most developers do not understand
  4. the rename command can rename any function/proc
把梦留给海 2024-08-01 16:33:07

我认为 time 命令非常棒。 它并没有完全隐藏,但这并不能阻止人们偶尔在 comp.lang.tcl 中询问“哪个函数更快”。

任何时候你想知道“这需要多长时间?” 或“哪种方法更快?” 您只需通过“time”命令调用它即可。 无需创建对象,无需数学运算,无需开销,非常简单。 其他语言也有类似的功能,尽管有些语言不太优雅。

I think the time command is wonderful. It's not exactly hidden but that doesn't stop people from asking "which function is faster" once in a while in comp.lang.tcl.

Anytime you want to know "how long does this take?" or "which method is faster?" you just call it via the "time" command. No creating of objects, no math, no overhead, exceptionally simple. Other languages have a similar feature, though some are a bit less elegant.

日裸衫吸 2024-08-01 16:33:07

记录完善的 C API 还可以轻松集成到 Perl 中。 我使用 Tcl/Tk 的经历可以追溯到 1995 年,但在 2000 年左右,我发现了 Perl/Tk 并且再也没有回头。

最近,Tcl 和 Tkx Perl 软件包为我们提供了具有现代外观的 GUI。 上述两个模块虽然并不微不足道,但考虑到它们允许人们跨语言边界执行的操作,涉及的代码相对较少。 这可以直接归功于优秀的 API(显然还有 Perl 的强大功能)。

The well documented C API also allowed easy integration in Perl. My experience with Tcl/Tk goes back to 1995, but in 2000 or so, I discovered Perl/Tk and never looked back.

And lately, the Tcl and Tkx Perl packages give us modern-looking GUI's. And the two aforementioned modules, while not trivial, involve relatively little code, considering what they allow one to do across language boundaries. And that can be directly attributable to the excellent API (and the power of Perl, obviously).

葬﹪忆之殇 2024-08-01 16:33:06

我最喜欢的“隐藏或方便的功能”是 Tcl 中引用的工作方式。 我喜欢用“引用是工具,而不是规则”这句话。 我还喜欢说“当你需要大括号时才需要大括号

虽然大多数语言都有规则规定哪些块分隔符必须用于某些事物(例如,在 C 中你必须使用 {}来指定一个块),Tcl 没有那么严格。

使用 Tcl,您可以选择任何引用字符来获得所需的效果。 当然有最佳实践,但最终您可以选择最能让您完成工作的引用字符。

这意味着,例如,您可以通过多种方式定义过程,包括:

proc foo {args} {
    .... body here ....
}

proc foo "args" "
    .... body here ....
"

proc foo args [some code that returns the body]

...等等。 条件语句、循环和其他一切也是如此。 (对于外行来说,大括号大致相当于 shell 单引号,双引号相当于 shell 双引号,方括号相当于 shell 反引号。)。

现在,很多人看到这个并说 WTF? 但它确实给了程序员很大的力量。 我们经常在 comp.lang.tcl 中收到这样的问题:“如果我执行‘this {and $that}’,我如何才能扩展 $that?”。 答案遵循老笑话“病人:医生,我做这个的时候很痛,医生:别这样做”。 也就是说,如果您不喜欢一组分隔符的行为,请选择其他分隔符。 仅仅因为 if 语句通常使用花括号构建并不意味着它必须使用花括号构建。

这是我最喜欢的 Tcl 的“隐藏”功能。 它不是隐藏的——它就在非常完整而简洁的 Tcl(n) 手册页上,但是直到您完全理解 Tcl 的工作原理之前,其后果并不清晰。

My favorite "hidden or handy feature" is how quoting works in Tcl. I like to use the phrase "quoting is a tool, not a rule". I also like to say "you only need curly braces when you need curly braces"

While most languages have rules for which block delimiters must be used for certain things (for example, in C you must use {} to designate a block), Tcl is not so stringent.

With Tcl, you can choose whatever quoting characters give you the effect you need. There are certainly best practices, but in the end you get to pick the quoting character that best lets you get the job done.

That means, for example, you can define a procedure in many ways, including:

proc foo {args} {
    .... body here ....
}

proc foo "args" "
    .... body here ....
"

proc foo args [some code that returns the body]

... and so on. Same goes for conditional statements, loops and everything else. (for the uninitiated, curly braces are roughly equivalent to the shell single quote, double quotes are like the shell double quote, and square brackets are like the shell backtick. ).

Now, many people look at that and say WTF? but it really gives a lot of power to the programmer. We often get questions in comp.lang.tcl along the lines of "if I do 'this {and $that}', how do I get $that to be expanded?". The answer follows the old joke "patient: doctor, it hurts when I do this doctor: don't do that". That is, if you don't like the behavior you get with one set of delimiters, choose some other delimiter. Just because an if statement is normally constructed with curly braces doesn't mean it must be constructed with curly braces.

That's my favorite "hidden" feature of Tcl. It's not hidden -- it's right on the wonderfully complete yet concise Tcl(n) man page, but the ramifications aren't clear until you fully grok how Tcl works.

杀手六號 2024-08-01 16:33:06

当 Sun 的营销人员宣称 Tcl 已“企业就绪”时,开发人员添加了以下功能:

$ tclsh
% clock format [clock seconds] -format %Q
Stardate 63473.2

When a marketing guy at Sun declared that Tcl was "Enterprise Ready", the developers added the following feature:

$ tclsh
% clock format [clock seconds] -format %Q
Stardate 63473.2
早乙女 2024-08-01 16:33:06

另一个不明显的功能是,无法识别的命令会被传递到一个名为“未知”的处理程序,您可以重新定义该处理程序。 例如。 将未知命令视为表达式进行评估:

$ tclsh
% 2+2
invalid command name "2+2"
% proc unknown args {uplevel 1 [linsert $args 0 expr]} 
% 2+2
4

更多示例可以在 wiki 页面上找到 Radical Language Modification

Another non-obvious feature is that unrecognised commands fall through to a handler called "unknown" which you can redefine. Eg. to have unknown commands treated as expressions to evaluate:

$ tclsh
% 2+2
invalid command name "2+2"
% proc unknown args {uplevel 1 [linsert $args 0 expr]} 
% 2+2
4

More examples can be found at the wiki page on Radical Language Modification

痴骨ら 2024-08-01 16:33:06

Tcl 的 [trace] 命令允许您拦截对任何变量的读取和写入。 这允许您在任何变量上实现观察者,并向任何变量添加任意复杂性的自动范围检查(就像您通过 setter/getter 访问变量一样)。 您还可以使用此技术创建自动递增变量。

proc varlimit_re {re var key op} {
  upvar $var v
  if { [regexp -- $re $v] <= 0 } {
    error "$var out of range"
  }
}

trace add variable ::myvar {write} [list varlimit_re {^[A-H]\d{3}-[0-9a-f]+$}]`

如果您尝试将“myvar”设置为与正则表达式不匹配的任何内容,您将收到运行时错误。

Tcl's [trace] command allows you to intercept reads and writes to any variable. This allows you to implement an observer on any variable, and to add automatic range checking of arbitrary complexity to any variable (as if you were accessing the variable via a setter/getter). You could also create auto-incrementing variables using this technique.

proc varlimit_re {re var key op} {
  upvar $var v
  if { [regexp -- $re $v] <= 0 } {
    error "$var out of range"
  }
}

trace add variable ::myvar {write} [list varlimit_re {^[A-H]\d{3}-[0-9a-f]+$}]`

If you try to set 'myvar' to anything that doesn't match the regular expression, you will get a runtime error.

余罪 2024-08-01 16:33:06

一个方便的功能不是隐藏的,但对于来自其他语言的人来说往往并不明显,那就是您可以定义自己的控制结构(如果您想危险地生活,甚至可以重新定义现有的控制结构)。 Tcl Wiki 上有示例

A handy feature which is not hidden but tends not to be obvious to people coming from other languages is that you can define your own control structures (or even redefine the existing ones if you want to live dangerously). There are examples on the Tcl Wiki

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