在TCL中编写程序
我对 TCL 还很陌生。只是我想知道如何编写无参数的TCL程序以及如何调用和执行它。
I am very new for TCL. Just I want to know that how to write TCL procedures without argument and how to call and how to execute it.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(11)
要编写一个不带任何参数的过程,请执行以下操作:
要调用该过程,只需写下其名称:
如果它返回一个值:
那么您可以通过将调用括在方括号中来对返回值执行某些操作,并将在您想要使用值的地方使用它:
执行它...取决于您的意思。我假设您的意思是从 Tcl 程序外部执行此操作。这是通过使整个脚本(例如,
theScript.tcl
)定义过程并执行调用来完成的,如下所示:然后运行如下所示:
To write a procedure that doesn't take any arguments, do this:
To call that procedure, just write its name:
If it was returning a value:
Then you'd do something with that returned value by enclosing the call in square brackets and using that where you want the value used:
To execute it... depends what you mean. I assume you mean doing so from outside a Tcl program. That's done by making the whole script (e.g.,
theScript.tcl
) define the procedure and do the call, like this:That would then be run something like this:
您可以像这样定义一个过程:
您可以通过简单地编写以下内容来执行它:
如果您想使用该过程的返回值,您可以这样做:
You can define a procedure like this:
And you can execute it by simply writing:
If you want to use a return value of the procedure, you can do:
Tcl 官方网站有一些有关函数(过程)的文档,可以在 https 上为您提供帮助://www.tcl.tk/man/tcl/TclCmd/proc.htm。
不带参数的过程
如果您不需要任何参数,这里是如何编写您想要的过程:
并且您可以按如下方式调用它:
带参数的过程
现在让我们说将来你需要争论。下面是在 TCL 中编写该过程的方法:
您可以通过执行以下操作来调用该函数:
这是一段代码供您尝试!
请记住在调用函数之前先定义函数,否则您将得到一个错误。
尝试将此代码复制粘贴到您的解释器中以开始使用它:
Te official Tcl website has some documentation on functions (procedures) that could help you at https://www.tcl.tk/man/tcl/TclCmd/proc.htm.
Procedure with no argument
If you don't need any argument here is how to write the procedure you want:
And you can call it as follows:
Procedure with arguments
Now let's say you need arguments in the future. Here is the way to write that precedure in TCL:
You can call that function by doing:
Here is a piece of code for you to try!
Remember to define functions before you call them, or you will get an error.
Try to copy paste this code in your interpreter to get started and play with it:
过程的语法
让我们看一下示例:
无参数:
如何运行:
第 1 步:在提示符下写入 tclsh
第 2 步:按照上述步骤编写程序
第 3 步:仅写入程序名称(即 Hello_eg)来运行程序
2。带参数:
如何运行此程序:
步骤 1:在提示符下写入 tclsh
步骤 2:按照上述步骤编写过程
步骤 3:仅写入带有参数的过程名称(即 Hello_Arg Ramakant Singla)以运行该过程
Syntax of procedure
Let See the Example:
Without Arguments:
How to run:
step 1: write tclsh on prompt
step 2: write the procedure as per above mention
step 3: write just the procedure name (i.e Hello_eg) to run the procedure
2.With Arguments:
How to run this:
step 1: write tclsh on prompt
step 2: write the procedure as per above mention
step 3: write just the procedure name with arguments (i.e Hello_Arg Ramakant Singla) to run the procedure
这很简单。
定义:
调用:
由于您是新手,我建议您阅读教程点。它们的内容简单而统一。
It's pretty simple.
Defining :
calling :
Since you are New, I advise you to go through tutorial point. They have simple and consolidated content.
过程是在程序中预先执行的一组语句。
语法
例如:
在上面的示例中......在过程中,我们为可以在主程序中调用的语句集提供了一个名称(
add
)。Procedure is a set of statements which is being preapeated in a program.
Syntax
Eg:
In the above example....in procedure we have provide a name (
add
) to the set of statements which can be call in the main program.任意数量的参数
使用
args
可能会派上用场。通过使用
args
,您可以将任意数量的参数传递给您的过程。可选参数
另一个技巧:用
{
}
将参数括起来使它们成为可选参数。默认值
如果您想要默认值,您可以按如下方式指定它们:
一些示例调用
Any amount of arguments
What maybe would come in handy is using
args
.By using
args
you can pass any amount of arguments to your procedure.Optional Arguments
Another tip: Enclosing arguments with
{
}
makes them optional arguments.Default Values
If you want to have default values you can specify them as follows:
Some Example Calls
除了上面的答案之外,我建议使用 tcltutor.exe(可从 http://tcltutor. software.informer.com/3.0b/)来学习TCL。
其中有一章关于子例程,将帮助您定义不带参数和带参数的 TCL 过程。
问候
沙拉德
In addition to the answers above, I would recommend using tcltutor.exe (available from http://tcltutor.software.informer.com/3.0b/) to learn TCL.
It'll have a chapter on Subroutines that'll help you define a TCL proc without and with arguments.
Regards
Sharad
要创建不带任何参数的 TCL 过程,您应该使用 proc 关键字,后跟过程名称,然后是过程范围。
您只需调用其名称即可使用创建的过程:
你好世界
To create a TCL procedure without any parameter you should use the proc keyword followed by the procedure name then the scope of your procedure.
You can use the created procedure by simply calling its name:
hello_world
这个解决方案是基于之前关于编写过程的问题。我个人认为这是在 tcl 中编写程序的更好方法之一。
代码
执行
This solution is based on previous questions about writing procs. I personally feel this is one of the better ways to write a procedure in tcl.
Code
Execution