在TCL中编写程序

发布于 2024-10-21 13:11:51 字数 49 浏览 3 评论 0原文

我对 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 技术交流群。

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

发布评论

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

评论(11

百善笑为先 2024-10-28 13:11:51

要编写一个不带任何参数的过程,请执行以下操作:

proc someName {} {
    # The {} above means a list of zero formal arguments
    puts "Hello from inside someName"
}

要调用该过程,只需写下其名称:

someName

如果它返回一个值:

proc example2 {} {
    return "some arbitrary value"
}

那么您可以通过将调用括在方括号中来对返回值执行某些操作,并将在您想要使用值的地方使用它:

set someVariable [example2]

执行它...取决于您的意思。我假设您的意思是从 Tcl 程序外部执行此操作。这是通过使整个脚本(例如,theScript.tcl)定义过程并执行调用来完成的,如下所示:

proc example3 {} {
    return "The quick brown fox"
}
puts [example3]

然后运行如下所示:

tclsh8.5 theScript.tcl

To write a procedure that doesn't take any arguments, do this:

proc someName {} {
    # The {} above means a list of zero formal arguments
    puts "Hello from inside someName"
}

To call that procedure, just write its name:

someName

If it was returning a value:

proc example2 {} {
    return "some arbitrary 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:

set someVariable [example2]

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:

proc example3 {} {
    return "The quick brown fox"
}
puts [example3]

That would then be run something like this:

tclsh8.5 theScript.tcl
野心澎湃 2024-10-28 13:11:51

您可以像这样定义一个过程:

proc hello_world_proc {} {
  puts "Hello world"
}

您可以通过简单地编写以下内容来执行它:

hello_world_proc

如果您想使用该过程的返回值,您可以这样做:

# Procedure declaration
proc hello_world_proc2 {} {
  return "Hello world"
}

# Procedure call
puts [hello_world_proc2]

You can define a procedure like this:

proc hello_world_proc {} {
  puts "Hello world"
}

And you can execute it by simply writing:

hello_world_proc

If you want to use a return value of the procedure, you can do:

# Procedure declaration
proc hello_world_proc2 {} {
  return "Hello world"
}

# Procedure call
puts [hello_world_proc2]
漫漫岁月 2024-10-28 13:11:51
proc myProc {} {
    # do something
}

# call proc
myProc
proc myProc {} {
    # do something
}

# call proc
myProc
暗恋未遂 2024-10-28 13:11:51

Tcl 官方网站有一些有关函数(过程)的文档,可以在 https 上为您提供帮助://www.tcl.tk/man/tcl/TclCmd/proc.htm

不带参数的过程

如果您不需要任何参数,这里是如何编写您想要的过程:

proc funcNameNoArgs {} {
    puts "Hello from funcNameNoArgs"
}

并且您可以按如下方式调用它:

funcNameNoArgs

带参数的过程

现在让我们说将来你需要争论。下面是在 TCL 中编写该过程的方法:

proc funcNameWithArgs {arg1 arg2 arg3} {
    puts "Hello from funcNameWithArgs "
}

您可以通过执行以下操作来调用该函数:

funcName arg1 arg2 arg3

这是一段代码供您尝试!

请记住在调用函数之前先定义函数,否则您将得到一个错误。

尝试将此代码复制粘贴到您的解释器中以开始使用它:

proc funcNameNoArgs {} {
    puts "Hello from a function with no arguments"
}
funcNameNoArgs
proc funcNameWithArgs {arg1 arg2 arg3} {
    puts "Hello from a function with 3 arguments"
    puts $arg1
    puts $arg2
    puts $arg3
}
funcNameWithArgs "Argument 1" "Argument 2" "Argument 3"

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:

proc funcNameNoArgs {} {
    puts "Hello from funcNameNoArgs"
}

And you can call it as follows:

funcNameNoArgs

Procedure with arguments

Now let's say you need arguments in the future. Here is the way to write that precedure in TCL:

proc funcNameWithArgs {arg1 arg2 arg3} {
    puts "Hello from funcNameWithArgs "
}

You can call that function by doing:

funcName arg1 arg2 arg3

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:

proc funcNameNoArgs {} {
    puts "Hello from a function with no arguments"
}
funcNameNoArgs
proc funcNameWithArgs {arg1 arg2 arg3} {
    puts "Hello from a function with 3 arguments"
    puts $arg1
    puts $arg2
    puts $arg3
}
funcNameWithArgs "Argument 1" "Argument 2" "Argument 3"
悲欢浪云 2024-10-28 13:11:51

过程的语法

proc <Name Of procedure> {No of arguments, if u want don't need simply left empty} { 
<Body>
 }

让我们看一下示例:

  1. 无参数:

    proc Hello_eg { } { 放置“Hello IM In procedure” }
    

如何运行:

第 1 步:在提示符下写入 tclsh

第 2 步:按照上述步骤编写程序

第 3 步:仅写入程序名称(即 Hello_eg)来运行程序

2。带参数:

proc Hello_Arg { first second }
{
   puts "The first argument is: $first"
   puts "The Second argument is: $second"
}

如何运行此程序:

步骤 1:在提示符下写入 tclsh

步骤 2:按照上述步骤编写过程

步骤 3:仅写入带有参数的过程名称(即 Hello_Arg Ramakant Singla)以运行该过程

Syntax of procedure

proc <Name Of procedure> {No of arguments, if u want don't need simply left empty} { 
<Body>
 }

Let See the Example:

  1. Without Arguments:

    proc Hello_eg { } { puts "Hello I M In procedure" }
    

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:

proc Hello_Arg { first second }
{
   puts "The first argument is: $first"
   puts "The Second argument is: $second"
}

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

美人骨 2024-10-28 13:11:51

这很简单。

定义:

proc myproc {} {

}

调用:

myproc 

由于您是新手,我建议您阅读教程点。它们的内容简单而统一。

It's pretty simple.

Defining :

proc myproc {} {

}

calling :

myproc 

Since you are New, I advise you to go through tutorial point. They have simple and consolidated content.

初见你 2024-10-28 13:11:51

过程是在程序中预先执行的一组语句。

语法

proc <Name> {INPUTS} {
BODY
}

例如:

proc add {m n} {
    set s 0
    set s [expr $m + $n]
    return $s
}

#Main Program Starts Here

set x 2
set y 3
set Result [add $x $y]
puts "$Result"

在上面的示例中......在过程中,我们为可以在主程序中调用的语句集提供了一个名称(add)。

Procedure is a set of statements which is being preapeated in a program.

Syntax

proc <Name> {INPUTS} {
BODY
}

Eg:

proc add {m n} {
    set s 0
    set s [expr $m + $n]
    return $s
}

#Main Program Starts Here

set x 2
set y 3
set Result [add $x $y]
puts "$Result"

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.

老娘不死你永远是小三 2024-10-28 13:11:51

任意数量的参数

使用 args 可能会派上用场。
通过使用args,您可以将任意数量的参数传递给您的过程。

proc withAnyNumberOfArguments {args} {
    if {$args eq ""} {
        puts "got no arguments"
    } 
    foreach arg $args {
        puts "got $arg"
    }
}

可选参数

另一个技巧:用 { } 将参数括起来使它们成为可选参数。

proc atLeastOneArgument {a1 {args}} {
    puts -nonewline "got a1=$a1"
    foreach arg $args {
        puts -nonewline " and $arg"
    }
    puts "."
}

默认值

如果您想要默认值,您可以按如下方式指定它们:

proc putsTime { {secondsSinceBeginOfEpoch "now"} } {
    if {$secondsSinceBeginOfEpoch eq "now"} {
        set secondsSinceBeginOfEpoch [clock seconds]
    }
    return [clock format $secondsSinceBeginOfEpoch]
}

一些示例调用

1 % withAnyNumberOfArguments
got no arguments

2 % withAnyNumberOfArguments one
got one

3 % withAnyNumberOfArguments ready steady go!
got ready
got steady
got go!


4 % atLeastOneArgument "this is one argument" ;# because its in double quotes
got a1=this is one argument.

5 % atLeastOneArgument 3 2 1 go!
got a1=3 and 2 and 1 and go!.


6 % puts [formatTime]
Fri Dec 18 16:39:43 CET 2015

7 % puts [formatTime 0]
Thu Jan 01 01:00:00 CET 1970    

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.

proc withAnyNumberOfArguments {args} {
    if {$args eq ""} {
        puts "got no arguments"
    } 
    foreach arg $args {
        puts "got $arg"
    }
}

Optional Arguments

Another tip: Enclosing arguments with { } makes them optional arguments.

proc atLeastOneArgument {a1 {args}} {
    puts -nonewline "got a1=$a1"
    foreach arg $args {
        puts -nonewline " and $arg"
    }
    puts "."
}

Default Values

If you want to have default values you can specify them as follows:

proc putsTime { {secondsSinceBeginOfEpoch "now"} } {
    if {$secondsSinceBeginOfEpoch eq "now"} {
        set secondsSinceBeginOfEpoch [clock seconds]
    }
    return [clock format $secondsSinceBeginOfEpoch]
}

Some Example Calls

1 % withAnyNumberOfArguments
got no arguments

2 % withAnyNumberOfArguments one
got one

3 % withAnyNumberOfArguments ready steady go!
got ready
got steady
got go!


4 % atLeastOneArgument "this is one argument" ;# because its in double quotes
got a1=this is one argument.

5 % atLeastOneArgument 3 2 1 go!
got a1=3 and 2 and 1 and go!.


6 % puts [formatTime]
Fri Dec 18 16:39:43 CET 2015

7 % puts [formatTime 0]
Thu Jan 01 01:00:00 CET 1970    
暮年 2024-10-28 13:11:51

除了上面的答案之外,我建议使用 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

睡美人的小仙女 2024-10-28 13:11:51

要创建不带任何参数的 TCL 过程,您应该使用 proc 关键字,后跟过程名称,然后是过程范围。

proc hello_world {} {
   // Use puts to print your output in the terminal.
   // If your procedure return data use return keyword.
}

您只需调用其名称即可使用创建的过程:
你好世界

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.

proc hello_world {} {
   // Use puts to print your output in the terminal.
   // If your procedure return data use return keyword.
}

You can use the created procedure by simply calling its name:
hello_world

卖梦商人 2024-10-28 13:11:51

这个解决方案是基于之前关于编写过程的问题。我个人认为这是在 tcl 中编写程序的更好方法之一。

代码

proc sampleProc args {

   # Defaults
   array set options {-device router0 -ip "10.16.1.62"}

   # Read args
   array set options $args

   # Assign
   set device $options(-device)
   set ip $options(-ip)

   # Usage
   puts "Device under use is $device and IP is  $ip"

   # Return 
   return "${sd} :: $ip"
}

执行

tclsh> source sampleProc.tcl
Device under use is router0 and IP is 10.16.1.62
router0 :: 10.16.1.62

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

proc sampleProc args {

   # Defaults
   array set options {-device router0 -ip "10.16.1.62"}

   # Read args
   array set options $args

   # Assign
   set device $options(-device)
   set ip $options(-ip)

   # Usage
   puts "Device under use is $device and IP is  $ip"

   # Return 
   return "${sd} :: $ip"
}

Execution

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