Reputation: 29
I am still confuse about eval and exac scenario as below;
1st scenario: exec ping "stackoverflow.com" -n 1
2nd scenario: eval exec [list ping //nologo "stackoverflow.com" -n 1]
3rd scenario: [list eval exec [list ping //nologo "stackoverflow.com" -n 1]]
The questions as below; 1. Difference tree above? 2. what is value number 1? 3. which one is good to use it?
Thanks in advance.
Upvotes: 2
Views: 1067
Reputation: 5763
Starting with Tcl 8.5 (current is 8.6.8), the expansion
operator {*}
(which breaks a list
into its component words) was added, and eval
is rarely needed except
when evaluating scripts and script fragments.
With older versions of Tcl, eval
is used instead of the expansion operator.
With the use of the expansion operator, #2 would become:
exec {*}[list ping /nologo "stackoverflow.com" -n 1]
There's nothing wrong with your #1, but there are a couple of common
patterns with the usage of exec
where #2 is more useful.
a) Saving the command to be executed allows you to reuse it for a retry or for debugging.
b) Commands can be built in a dynamic fashion.
foreach {host} [list stackoverflow.com stack_typo_exchange.com superuser.com] {
set cmd [list ping /nologo $host -n 1]
try {
exec {*}$cmd
} on error {err res} {
puts "ERROR: exec: $cmd"
puts " result: $res"
}
}
Older versions of Tcl would use the catch
command:
if { [catch {eval exec $cmd}] } {
puts "ERROR: exec: $cmd"
}
Your #3 is (usually) not correct code. It is creating a list out of the return value from eval exec
.
References: Tcl / argument expansion, try, catch, exec
Upvotes: 1