TCL subst or eval command is not working in my case ..

前端 未结 3 2070
醉酒成梦
醉酒成梦 2021-01-26 10:30
subst or eval command is not working in my case .. 

% proc sum {a b} {
    return [expr $a+$b]
}
%
% set a 1
1
% set b 2
2
% sum $a $b
3

%
% sum {$a} {$b}
can\'t use n         


        
3条回答
  •  无人共我
    2021-01-26 10:48

    First, you do understand why “$a“ isn't a value you can add to? It's not a number at all. (I don't mean $a, the instruction to read from a variable and substitute it, I mean the string consisting of a $ followed by an a.)

    When you put braces round things in Tcl, it means “don't do anything with this string at all; use it as-is”. Always. (Sometimes you're feeding that string into a command that evaluates, but not always.) When you put square brackets round things, it means evaluate that string as a script immediately and use the result of the script as the value to substitute. Always.

    When you do:

    subst [sum {$a} {$b}]
    

    You need to understand that the call to sum is done while assembling the arguments to subst. That call produces an error, so the call to subst never happens. Similarly with the eval form you used.

    If we use a somewhat less surprising form:

    subst {sum {$a} {$b}}
    

    Then you'll get this out: sum {1} {2}. subst doesn't understand the overall string as a script. On the other hand, with:

    eval {sum {$a} {$b}}
    

    In this case you get an error not from the eval as such, but rather from the fact that the call to sum inside is still erroneous.

    I suppose you could do:

    eval [subst {sum {$a} {$b}}]
    

    But really don't. There's got to be a simpler and less error-prone way.

提交回复
热议问题