Mike Mike - 1 year ago 180
Bash Question

ifdef equivalent in TCL

In TCL, is there an elegant implementation that will mimic `ifdef frequently used in programming languages. From TCL scripting language itself, I couldn't find a way that is natively supported by TCL interpreter.

Answer Source

There's nothing built in to Tcl that is equivalent to "ifdef", except for the standard "if" statement. If you don't like the standard "if" statement, tcl gives you all of the fundamental building blocks to create your own "ifdef" command.

For example, ifdef is really nothing more than an if statement looking for a defined value. A really simple implementation might look something like this:

proc define {name value} {
    set ::defined($name) $value

proc ifdef {name script1 else script2} {
    if {[info exists ::defined($name)]} {
        uplevel $script1
    } else {
        uplevel $script2

You could then use it like this:

define FOO 1
ifdef FOO {
    <do this if FOO is defined>
} else {
    <do this if FOO is NOT defined>
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download