ZetCode

Tcl eval Command

last modified April 3, 2025

The Tcl eval command evaluates one or more Tcl scripts. It's a powerful command that allows dynamic execution of Tcl code. The command concatenates its arguments and evaluates the result as a Tcl script.

Basic Definition

The eval command takes one or more arguments, joins them together with spaces, and evaluates the result as a Tcl script. It's useful for constructing and executing commands dynamically.

Syntax: eval arg ?arg ...?. The arguments are concatenated and evaluated as a Tcl script. The return value is the result of the evaluation.

Basic eval Usage

This example shows the simplest usage of eval to execute a command.

basic_eval.tcl
set cmd "puts"
set arg "\"Hello, Tcl!\""
eval $cmd $arg

Here we construct a command dynamically by storing the command name and its argument in variables. The eval command then executes the complete command.

Evaluating Multiple Commands

eval can evaluate multiple commands at once when they're combined into a single script.

eval_multiple.tcl
set script {
    set x 10
    set y 20
    expr {$x + $y}
}
set result [eval $script]
puts "The result is $result"

This example demonstrates how eval can execute a block of Tcl code stored in a variable. The script sets two variables and returns their sum.

Building Commands Dynamically

eval is particularly useful when you need to build commands dynamically based on runtime conditions.

eval_dynamic.tcl
proc calculate {operation args} {
    set cmd "expr {[join $args $operation]}"
    eval $cmd
}

set sum [calculate + 5 10 15]
puts "Sum: $sum"

This creates a flexible calculator procedure that builds an expression based on the provided operation. The eval command executes the dynamically constructed expression.

Handling Command Substitution

eval properly handles command substitution and variable expansion when building commands.

eval_substitution.tcl
set name "John"
set age 30
set cmd "puts"
set args [list "Name: $name, Age: $age"]

eval $cmd $args

This example shows how eval processes variable substitution in the arguments before executing the command. The list command helps maintain proper argument grouping.

Evaluating Lists as Commands

eval can evaluate a properly formatted list as a command with its arguments.

eval_list.tcl
set command [list puts "Hello from a list!"]
eval $command

set math [list expr 10 + 20 * 2]
set result [eval $math]
puts "Math result: $result"

Here we construct complete commands as lists and execute them with eval. This technique is useful for building commands programmatically while maintaining proper argument separation.

Safe Evaluation with try

When using eval with untrusted input, it's good practice to use error handling.

eval_safe.tcl
set userInput "expr {10 / 0}" ;# Dangerous input

try {
    set result [eval $userInput]
    puts "Result: $result"
} trap {ARITH DIVZERO} {msg} {
    puts "Error: Division by zero"
} on error {msg} {
    puts "Evaluation error: $msg"
}

This demonstrates safe evaluation of potentially dangerous input. The try command catches arithmetic errors and other evaluation problems that might occur.

Best Practices

This tutorial covered the Tcl eval command with practical examples showing its usage in different scenarios.

Author

My name is Jan Bodnar, and I am a passionate programmer with extensive programming experience. I have been writing programming articles since 2007. To date, I have authored over 1,400 articles and 8 e-books. I possess more than ten years of experience in teaching programming.

List all Tcl Tutorials.