my_question
my_question

Reputation: 3235

How to maintain argument type in eval command

Let's say I have this:

set arg {{e1 e2}}
eval puts $arg

Because eval command concatenates its arguments first, $arg becomes {e1 e2}.

How can I keep the argument intact in calling eval? In the above example, I want $arg still be {{e1 e2}}.

Specifically, my situation is like this:

proc p1 {cmd arg} {
  eval $cmd $arg
}

So both the command name and its argument are passed into this proc. Command name is always a literal value, so it is not a problem. The key is I want the argument'e data integrity intact in calling eval.

Upvotes: 0

Views: 79

Answers (2)

Dinesh
Dinesh

Reputation: 16428

% set arg {{e1 e2}}
{e1 e2}
% eval { puts $arg }
{e1 e2}
% # Or use 'list' command to make them not to be get expanded.
% eval [ list puts $arg ] 
{e1 e2}
% 

Applying this logic as per your proc, it can be anything of the following

% proc p1 { cmd arg } {
    eval { $cmd $arg } 
}
% p1 puts {{e1 e2}}
{e1 e2}
%     
% proc p2 { cmd arg } { 
    eval [ list $cmd $arg ] 
}
% p2 puts {{e1 e2}}
{e1 e2}
% proc p3 { cmd arg } { 
    eval $cmd [ list $arg ] 
}
% p3 puts {{e1 e2}}
{e1 e2}
%

Upvotes: 1

sankar guda
sankar guda

Reputation: 69

Try to give command name and its argument in double quotes separately.

Tcl will consider them as two separate strings.

Upvotes: 0

Related Questions