user2850271
user2850271

Reputation:

Create a main method in tcl?

I want to centralize the execution of various methods by calling them from a single method, like a main method in java..is there a way to do this in tcl?

I've looked everywhere for the answer but so far no luck. An another way to go about this is to setup flags within the functions so the code block is only executed when the flag has a specific value but this is a tedious process since there a lot of procedures in my program.

Any help is greatly appreciated. Thanks

Example (pseudo-code):

func1 () {...}
func2 () {...}

main method () {
  int a, b
  func1(a)
  func2(b)
}

Upvotes: 1

Views: 1316

Answers (2)

Donal Fellows
Donal Fellows

Reputation: 137687

One way of doing something main-like is to use a lambda application. If you're doing this, it is often helpful to pass in the script arguments.

# Usual stuff at the top of a script
pacakge require Tcl 8.5
package require pkgABC
package require pkgDEF

proc GHI {} { ... }

apply {{a b args} {
    # This part now works like main() in C or Java or ...
}} {*}$argv

Upvotes: 2

Dmitry Ledentsov
Dmitry Ledentsov

Reputation: 3660

Tcl can be seen as a scripting language. The body of the Tcl script is the main method. Thus, you don't really need a main method. Calling procedures from procedures is standard functionality:

proc func1 {} {
    puts A
}

proc func2  {} {
    puts B
}

proc Main {} {
    func1
    func2
}

Main

A
B

Main has to be called explicitly in order for func1 and func2 be executed.

Just putting the Main contents into the script body produces the same result

Upvotes: 2

Related Questions