【问题标题】:how to call functions from a tcl main script?如何从 tcl 主脚本调用函数?
【发布时间】:2020-05-08 00:29:12
【问题描述】:

我正在阅读有关包装的 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"
}

来自主:

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$ tclsh main.tcl 
hello world
7
wrong # args: should be "tutstack::hello"
    while executing
"tutstack::hello "fred""
    invoked from within
"puts [tutstack::hello "fred"]"
    (file "main.tcl" line 15)
thufir@dur:~/NetBeansProjects/spawnTelnet/telnet$ 

【问题讨论】:

  • 您已将hello 定义为不接受任何参数。 helloWorld 接受一个参数。

标签: syntax package tcl proc method-call


【解决方案1】:

哎呀:

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::helloWorld "fred"]

感谢格伦

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2023-03-17
    • 1970-01-01
    • 2011-05-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多