繁体   English   中英

如何在其他 tcl 脚本中运行 tcl 脚本?

[英]How to run tcl script inside other tcl script?

我有两个 tcl 脚本。 我想在第一个脚本完成后运行第二个脚本。 我该怎么做?

取决于你的真正意思。

一种方法是编写第三个(“主”)脚本

source /the/path/to/the/first.tcl
source /the/path/to/the/second.tcl

另一种方法是将上述示例中对source的第二个调用添加到第一个脚本的底部。

修正案第一种方法:如果要执行的脚本位于同一目录中的主脚本,一个惯用的方式来source他们是

set where [file dirname [info script]]
source [file join $where first.tcl]
source [file join $where second.tcl]

无论当前流程的目录是什么以及项目目录位于何处,这种方式的采购都将起作用。

虽然这通常是一个正确的答案,因为问题没有被精确地表述出来,有很多方法可以实现从 Tcl 中运行 Tcl 代码的目标。 我想详细讨论这个,因为理解代码的执行是理解 Tcl 本身的一个重点。

source

source命令不应该与以经典方式执行脚本混淆,我认为线程启动器已经提出了这一要求。

source 命令类似于 c/perl/php 中的“include”命令。 另一方面,像 java 或 python 这样的语言只有“导入”机制。

不同之处在于这些语言创建了可用包的内部数据库,这些包链接到相应的源/二进制/字节码文件。 通过编写导入语句,链接的源或字节码或二进制文件被加载。 这允许更深入的依赖管理,而无需编写额外的代码。 在 Tcl 中,这可以通过命名空间和package require命令来实现。 例子:

假设你有这个 source.tcl:

proc foo {bar} {puts "baz"}
set BAM "BOO"

现在,您拥有了您所说的“主”脚本。 我称之为“主要”。 它有以下内容:

set BAM {my important data}
source source.tcl
#also the function foo can now be used because the source reads the whole script
foo {wuz}
set BAM
#will output "BOO"

exec命令

如果您可以忍受启动全新解释器实例的额外开销,您还可以执行以下操作:

set BAM {my important data}
exec tclsh source.tcl
#The variable BAM will not be modified. You can not use the function foo.

eval命令

命令eval可以像编程代码一样评估字符串或列表(在 Tcl 中,一切都是字符串)。 您必须将完整的源文件加载到字符串中。 然后使用eval来评估单独范围内的代码,不要覆盖主源文件中的内容。

set fp [open "somefile" r]
set code_string [read $fp]
close $fp
eval $code_string

您只需要使用 source 来运行第二个脚本。

source "/tmp/whatever.tcl"

我能找到的最简单的工作示例:

thufir@dur:~/NetBeansProjects/spawnTelnet/telnet$ 
thufir@dur:~/NetBeansProjects/spawnTelnet/telnet$ tclsh main.tcl 
hello world
7
thufir@dur:~/NetBeansProjects/spawnTelnet/telnet$ 
thufir@dur:~/NetBeansProjects/spawnTelnet/telnet$ cat main.tcl 
lappend auto_path /home/thufir/NetBeansProjects/spawnTelnet/telnet/api

package require weather 1.0



tutstack::hello

set A 3
set B 4

puts [tutstack::sum $A $B]


#puts [tutstack::hello "fred"]



thufir@dur:~/NetBeansProjects/spawnTelnet/telnet$ 
thufir@dur:~/NetBeansProjects/spawnTelnet/telnet$ cat api/weather.tcl 
package provide weather  1.0
package require Tcl      8.5

namespace eval ::tutstack {
}

proc ::tutstack::hello {} {
    puts "hello world"
}


proc ::tutstack::sum {arg1 arg2} {
    set x [expr {$arg1 + $arg2}];
    return $x
}

proc ::tutstack::helloWorld {arg1} {
    return "hello plus arg"
}
thufir@dur:~/NetBeansProjects/spawnTelnet/telnet$ 
thufir@dur:~/NetBeansProjects/spawnTelnet/telnet$ cat api/pkgIndex.tcl 
# Tcl package index file, version 1.1
# This file is generated by the "pkg_mkIndex" command
# and sourced either when an application starts up or
# by a "package unknown" script.  It invokes the
# "package ifneeded" command to set up package-related
# information so that packages will be loaded automatically
# in response to "package require" commands.  When this
# script is sourced, the variable $dir must contain the
# full path name of this file's directory.

package ifneeded weather 1.0 [list source [file join $dir weather.tcl]]
thufir@dur:~/NetBeansProjects/spawnTelnet/telnet$ 

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM