Mike Mike - 2 days ago 4
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

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>
}
Comments